微信小程序之swiper組件高度自適應


swiper默認最小高度150

要求:swiper高度自適應

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;
}

wxjs

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 }) },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM