微信小程序(八)实战——加载图片images

news/2024/7/6 6:29:44

1.加载本地图片

本地路径:/pages/images/1.png

<image class="widget_arrow" src="/pages/images/1.png" mode="aspectFill"></image>

在wxss中设置图片样式

.widget_arrow{
    width:25px; 
    height:25px; 
}

2.加载网络图片

网络图片地址:http://img1.3lian.com/2015/w7/85/d/101.jpg

<image class="widget_arrow2" src="http://img1.3lian.com/2015/w7/85/d/101.jpg" mode="aspectFill"></image>
在wxss中设置图片样式

.widget_arrow2{
    width:155px; 
    height:155px; 
}
结果:

3.点击选择-加载图片

3-1:我们定义的全局变量,也就是data中的url,已经有值了,现在只需要在页面中显示即可。
//点击此按钮调用选择图片的方法,成功后将图片显示在image标签上
<button bindtap="bindViewTap" type="submit">点击我显示图片</button>
<image src="{{avatarUrl}}" class="widget_arrow3"></image>

3-2:wx.chooseImage({})此方法是用来选择图片的方法

bindViewTap: function () {
    var that = this;
    wx.chooseImage({
      // 设置最多可以选择的图片张数,默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
      count: 1,
      sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
      success: function (res) {
        // 获取成功,将获取到的地址赋值给临时变量
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          //将临时变量赋值给已经在data中定义好的变量
          avatarUrl: tempFilePaths
        })
      },
      fail: function (res) {
        // fail
    },
      complete: function (res) {
        // complete
    }
    })
  },

3-3:点击后选择,再显示

1


2

4.加载多张图片

4-1:首先在data中定义好数据源:

data: {
    avatarUrl: null,
    pictures: 
    ['/pages/images/1.png',
        '/pages/images/2.png',
        '/pages/images/3.png',
    ]
  },

4-2:然后创建方法previewImage,实现图片预览:

previewImage: function (e) {
    var that = this,
      //获取当前图片的下表
      index = e.currentTarget.dataset.index,
      //数据源
      pictures = this.data.pictures;
    wx.previewImage({
      //当前显示下表
      current: pictures[index],
      //数据源
      urls: pictures
    })
  },

4-3:然后再页面中边遍历数据,显示:

<view>
 <image wx:for="{{pictures}}" wx:key="unique"src="{{item}}" data-index="{{index}}" bindtap="previewImage" class="widget_arrow3"></image>
</view>

4-4:可以在wxss中设置样式

.widget_arrow3{
    width:55px; 
    height:55px; 
}


http://www.niftyadmin.cn/n/3272120.html

相关文章

更改应用程序图标无效

使用Markdown编辑器 今天看了下之前写的小程序&#xff0c;想要给它换一个图标&#xff0c;在application里做了设置如下&#xff0c;结果发现不起作用&#xff0c;我确定文件名称没有写错&#xff0c;文件也放在了它该在的位置。结果就是在手机桌面显示的还是那个机器人的小图…

微信小程序(十一)实战——时间的获取,比较,判断(微信小程序 如何获取时间)

1.获取当前系统日期和时间 在小程序中&#xff0c;新建项目时&#xff0c;就会有一个utils.js文件&#xff0c;就是获取日期和时间的&#xff0c;代码如下&#xff1a; utils.js&#xff1a;const formatTime date > {const year date.getFullYear()const month date.get…

Android退出应用的方式

思路有这么几种&#xff1a; 1.把启动的activity都放入一个栈中&#xff0c;当要退出应用的时候遍历关闭 2.广播方式&#xff0c;基类的activity中添加广播接收器&#xff0c;接收到广播就关闭activity&#xff0c;其他activity继承这个activity&#xff0c;关闭时发送广播 …

javascript三级联动效果实现2

var text ""; for (i 0; i < data.length; i) {text "<option value" i " data-pId" data[i].pId ">" data[i].pName "</option>"; } $(".shengf").append(text); //step2:城市 var text…

微信小程序(十二)实战——小程序模板template的使用,以及传递集合数据

如下图&#xff0c;我们经常做这样的列表页&#xff0c;课程搜索结果页和课程列表页结构是完全一样的&#xff0c;非常适合使用模板来完成页面搭建。 这样我们就不用写那些重复的代码了,而且修改界面的时候也只需要改动模板一个地方 WXML提供模板&#xff08;template&#xff…

docker基础入门之二

一、docker文件系统&#xff1a; linuxFS包括boot file system 和 root file system boot file system (bootfs)&#xff0c;包含bootloader和kernel&#xff0c;在系统启动完成后&#xff0c;kernel滞留内存&#xff0c;bootfs解除挂载&#xff1b; root file system&#xff…

Google浏览器Chrome使用技巧

Google浏览器Chrome使用技巧 Google浏览器Chrome使用技巧一标签页快捷操作二Tab智能搜索三地址栏快捷键 整理一下网上的chrome使用技巧。虽然经常使用chrome&#xff0c;但是再知道几个小技巧说不定会提高效率。 一、标签页快捷操作 直接点击关闭按钮&#xff0c;chrome会自…

微信小程序(十三)实战——Form表单提交

官方手册&#xff1a; 实例走一波 >>> form.wxml <!--pages/form/form.wxml--> <text>pages/form/form.wxml</text> <view class"viewTitle"> <text class"view-Name">form表单</text> <view class&…