微信小程序?qū)崿F(xiàn)輪播圖(適配機型)
本文實例為大家分享了微信小程序?qū)崿F(xiàn)輪播圖的具體代碼,供大家參考,具體內(nèi)容如下
1、wxss樣式:
/* 頁面顯示圖片的框 */ .showTableBox { ? ? position: relative; ? ? width: 100%; ? ? height: 180px; ? ? overflow: hidden; } /* img圖片所在的父元素盒子,有幾張img,width就設(shè)置百分之幾百 */ .slideshowImgBox { ? ? position: absolute; ? ? z-index: 1; ? ? width: 500%; ? ? height: 100%; } /* img width設(shè)置為: 100% / 圖片總數(shù) 的百分比 */ .slideshowImgBox image { ? ? width: 20%; ? ? height: 100%; ? ? float: left; } /* 下方小圓點樣式*/ .circleBox { ? ? width: 100%; ? ? position: absolute; ? ? bottom: 10px; ? ? justify-content: center; } .circle { ? ? width: 5px; ? ? height: 5px; ? ? border-radius: 50%; ? ? background-color: #F5F5F5; ? ? margin: 0 5px; ? ? z-index: 999; } .circleActive { ? ? background-color: #BF360C; }
其實到這里適配就完成啦,下面是怎么手寫一個輪播圖哈哈
2、js代碼:
page({ ? data: { ? ? ? ? leftNum: 0,//type為number的偏移量,方便計算 ? ? ? ? imgLeft: "0", //偏移量, ? ? ? ? screenWidth: 0, //屏幕width ? ? ? ? changCircleIndex: 0, //對應(yīng)圓球的下標,css根據(jù)頁面下標顯示對應(yīng)class ? ? ? ? slideshowImgs: ["../images/beiJing.jpg", "../images/chengDu.jpg", ? ? ? ? ? ? "../images/shangHai.jpg", "../images/chongQing.jpg", "../images/beiJing.jpg" ? ? ? ? ], }, ?onLoad: function (options) { ? ? ? ? this.setData({ ? ? ? ? ? ? screenWidth: wx.getSystemInfoSync().windowWidth //獲取屏幕寬度 ? ? ? ? }) ? ? ? ? this.imageCarousel() ? ? }, ? ? imageCarousel: function () { ? ? ? ? let imgOverallWidth = this.data.screenWidth * (this.data.slideshowImgs.length - 1) ? ? ? ? let timer = setInterval(() => { ? ? ? ? ? ? this.data.imgLeft = `${this.data.leftNum -= this.data.screenWidth}px` ? ? ? ? ? ? this.setData({ ? ? ? ? ? ? ? ? imgLeft: this.data.imgLeft, ? ? ? ? ? ? ? ? changCircleIndex: -(this.data.leftNum / this.data.screenWidth) ? ? ? ? ? ? }) ? ? ? ? ? ? if (this.data.leftNum === -imgOverallWidth) { ? ? ? ? ? ? ? ? this.setData({ ? ? ? ? ? ? ? ? ? ? changCircleIndex: 0, ? ? ? ? ? ? ? ? ? ? leftNum: 0, ? ? ? ? ? ? ? ? ? ? imgLeft: `${this.data.leftNum}px` ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? } ? ? ? ? }, 3000) ? ? }, })
3、wxml代碼:
<view class="photo"> ?? ?<view class="photoFixedBox"> ?? ??? ?<view class="showTableBox"> ?? ??? ??? ?<view class="slideshowImgBox" style="left:{{imgLeft}};"> <!--這里列表渲染的圖片一共五張,第一張和最后一張是同一張圖,防止輪播時出現(xiàn)白屏 --> ?? ??? ??? ??? ?<image wx:for="{{slideshowImgs}}" src="{{item}}"></image> ?? ??? ??? ?</view> ?? ??? ??? ?<view class="circleBox flex"> ?? ??? ?<!-- {{changCircleIndex === index ? 'circleActive' : ''}}"是動態(tài)class,根據(jù)index來改變圓點顏色 ?--> ?? ??? ??? ??? ?<view wx:for="{{slideshowImgs.length-1}}" class="circle {{changCircleIndex === index ? 'circleActive' : ''}}"></view> ?? ??? ??? ?</view> ?? ??? ?</view> ?? ??? ?<view class="photoClassifyBigBOx"> ?? ??? ??? ?<view class="imgClassifyBox flex" ?wx:for="{{imgClassify}}"><text>{{item}}</text></view> ?? ??? ?</view> ?? ?</view> </view>
以上就是整個輪播圖的實現(xiàn)啦,圓點的變色顯示index根據(jù)圖片left的偏移量距離來計算:偏移量 / 顯示框的寬度,
其實微信小程序有swiper組件,使用也是很方便的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
layer插件實現(xiàn)在彈出層中彈出一警告提示并關(guān)閉彈出層的方法
今天小編就為大家分享一篇layer插件實現(xiàn)在彈出層中彈出一警告提示并關(guān)閉彈出層的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript判斷一個URL鏈接是否有效的實現(xiàn)方法
如何用javascript來判斷請求的url/鏈接有效(可連接,可用)?需要的朋友可以參考下。2011-10-10