微信小程序swiper高度自适应

<!--滚动图片-->
<view class='swiper'>
<swiper current="{{img.current}}" indicator-dots="true" indicator-color="#fff" indicator-active-color="#f23030" vertical="{{img.vertical}}" autoplay="{{img.autoplay}}" duration="{{img.duration}}" interval='{{img.interval}}' bindchange="bindchange" circular="{{img.circular}}" style="height:{{img.imgheights[img.current]}}rpx;">
<block wx:for='{{img.imgList}}' wx:key="{{index}}">
<swiper-item>
<image src="{{item}}" data-id='{{index}}' class="slide-image" mode="widthFix" bindload="imageLoad" />
</swiper-item>
</block>
</swiper>
</view>
<!--//滚动图片-->
data: {
    img: {
      //图片地址
      imgList: [
        'http://192.168.1.9:809/content/images/thumbs/000/0000117.png',
        'http://192.168.1.9:809/content/images/thumbs/000/0000121.png',
        'http://192.168.1.9:809/content/images/thumbs/000/0000116.png'
      ],
      //是否采用衔接滑动  
      circular: true,
      //是否竖直  
      vertical: false,
      //是否自动切换  
      autoplay: true,
      //自动切换的间隔
      interval: 3000,
      //滑动动画时长毫秒  
      duration: 100,
      //所有图片的高度  
      imgheights: [],
      //图片宽度 
      imgwidth: 750,
      //默认  
      current: 0
    }
  }, 
  //滚动图自适应高度
  imageLoad: function(e) { 
    //获取图片真实宽度   
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight; 
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var img = this.data.img;
    var imgheights = this.data.img.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    img.imgheights[e.target.dataset.id] = imgheight;
 
    this.setData({
      img: img
    }) 
  },
  bindchange: function(e) { 
    var img = this.data.img;
    img.current = e.detail.current;
    this.setData({
      img: img
    })
  },

 

posted @ 2019-04-19 11:48  evemen  阅读(636)  评论(0)    收藏  举报