【微信小程序開發】(轉)微信小程序實現banner圖輪播(動態獲取數據),自動獲取圖片高度

效果;
在這裏插入圖片描述
indicator-active-color="#007aff"//當前選中的指示點顏色

js:

Page({
  data: {
     //-----------模擬banner圖-----------
    imgUrls: [
      '/image/1545118381903.jpg',
      '/image/1545118566631.jpg'
    ],
    circular: true,
    //是否顯示畫板指示點  
    indicatorDots: true,
    //選中點的顏色  
    //是否豎直  
    vertical: false,
    //是否自動切換  
    autoplay: true,
    //自動切換的間隔
    interval: 3000,
    //滑動動畫時長毫秒  
    duration: 1000,
    //所有圖片的高度  
    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 })
  },
   }
})


wxml:

<!-------------首頁輪播圖-----------------  -->
<view class='swiper'>
  <swiper indicator-dots="{{indicatorDots}}" vertical="{{vertical}}" autoplay="{{autoplay}}" duration="{{duration}}" interval='{{interval}}'indicator-active-color="#007aff" bindchange="bindchange" circular="{{circular}}" style="height:{{imgheights[current]}}rpx;">
    <block wx:for='{{imgUrls}}' 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;
}
 
.swiper-image {
  height: 100%;
  width: 100%;
}
 
.slide-image {
  height: 100%;
  width: 100%;
  display: block;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章