小程序swiper組件高度自適應【轉載】
阿新 • • 發佈:2018-12-07
ima list fun ges detail rcu bind rdo load
最近在做小程序開發,復制官方文檔上的swiper組件實測後發現,圖片不能自適應。網上找了幾個版本測試都發現存在一些小問題,目前這個版本本人實測是最好用的。記錄一下,方便日後使用。
感謝原創大神的幫助,文章地址:https://www.cnblogs.com/wangyihong/p/8610956.html
wxml:
<view class=‘swiper‘> <swiper indicator-dots="{{indicatorDots}}" vertical="{{vertical}}" autoplay="{{autoplay}}" duration="{{duration}}"interval=‘{{interval}}‘ bindchange="bindchange" circular="{{circular}}" style="height:{{imgheights[current]}}rpx;"> <block wx:for=‘{{imgList}}‘ wx:key="{{index}}"> <swiper-item> <image src="{{item}}" data-id=‘{{index}}‘ class="slide-image" mode="widthFix" bindload="imageLoad"/> </swiper-item> </block> </swiper> </view>
wxss:
.swiper image {
width: 100%;
height: auto;
}
js:
data: { //圖片地址 imgList: [‘/images/wyh-img_bg.png‘, ‘/images/wyh-img8.png‘, ‘/images/wyh-img_shop1.png‘, ‘/images/wyh-img_bg1.png‘], //是否采用銜接滑動 circular: true, //是否顯示畫板指示點 indicatorDots: false, //選中點的顏色 indicatorcolor: "#000", //是否豎直 vertical: false, //是否自動切換 autoplay: true, //自動切換的間隔 interval: 2500, //滑動動畫時長毫秒 duration: 100, //所有圖片的高度 imgheights: [], //圖片寬度 imgwidth: 750, //默認 current: 0 }, imageLoad: function (e) {//獲取圖片真實寬度 var imgwidth = e.detail.width, imgheight = e.detail.height, //寬高比 ratio = imgwidth / imgheight; console.log(imgwidth, imgheight) //計算的高度值 var viewHeight = 750 / ratio; var imgheight = viewHeight; var imgheights = this.data.imgheights; //把每一張圖片的對應的高度記錄到數組裏 imgheights[e.target.dataset.id] = imgheight; this.setData({ imgheights: imgheights }) }, bindchange: function (e) { // console.log(e.detail.current) this.setData({ current: e.detail.current }) },
小程序swiper組件高度自適應【轉載】