本文實例為大家分享了微信小程序實現banner圖輪播的具體代碼,供大家參考,具體內容如下
效果圖:
indicator-active-color="#007aff"http://修改選中圖片圓點的顏色
js:
const app = getApp() 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; }
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com