微信小程序swiper组件实现图片宽度自适应

来源:互联网 发布:好看的网络爱情小说 编辑:程序博客网 时间:2024/06/10 04:24

转自 http://blog.csdn.net/xz_src/article/details/73827917

微信小程序 图片宽度自适应的实现

实例代码:

wxml 代码:

[html] view plain copy
  1. <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">   
  2.   <block wx:for="{{imgUrls}}" wx:key="image">   
  3.    <swiper-item>   
  4.      <image src="{{item.image}}" model="aspectFit" style="width: {{imageWidth}}px;" bindload="imageLoad" />   
  5.    </swiper-item>   
  6.   </block>   
  7. </swiper>   
JS 代码:
[javascript] view plain copy
  1. imageLoad: function () {   
  2.   this.setData({   
  3.    imageWidth: wx.getSystemInfoSync().windowWidth,//图片宽度   
  4.     
  5.    imgUrls: [   
  6.      { image: "http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg" },   
  7.    { image: "http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg" },   
  8.    { image: "http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg" }],   
  9.    indicatorDots: false,//是否显示圆点   
  10.   autoplay: true,//自动播放   
  11.   interval: 2000,//间隔时间   
  12.   duration: 1000//监听滚动和点击事件   
  13.  })   
  14. }  
原创粉丝点击