微信小程序?qū)崿F(xiàn)輪播圖(適配機(jī)型)
本文實(shí)例為大家分享了微信小程序?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; } /* 下方小圓點(diǎn)樣式*/ .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; }
其實(shí)到這里適配就完成啦,下面是怎么手寫一個(gè)輪播圖哈哈
2、js代碼:
page({ ? data: { ? ? ? ? leftNum: 0,//type為number的偏移量,方便計(jì)算 ? ? ? ? imgLeft: "0", //偏移量, ? ? ? ? screenWidth: 0, //屏幕width ? ? ? ? changCircleIndex: 0, //對(duì)應(yīng)圓球的下標(biāo),css根據(jù)頁面下標(biāo)顯示對(duì)應(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}};"> <!--這里列表渲染的圖片一共五張,第一張和最后一張是同一張圖,防止輪播時(shí)出現(xiàn)白屏 --> ?? ??? ??? ??? ?<image wx:for="{{slideshowImgs}}" src="{{item}}"></image> ?? ??? ??? ?</view> ?? ??? ??? ?<view class="circleBox flex"> ?? ??? ?<!-- {{changCircleIndex === index ? 'circleActive' : ''}}"是動(dòng)態(tài)class,根據(jù)index來改變圓點(diǎn)顏色 ?--> ?? ??? ??? ??? ?<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>
以上就是整個(gè)輪播圖的實(shí)現(xiàn)啦,圓點(diǎn)的變色顯示index根據(jù)圖片left的偏移量距離來計(jì)算:偏移量 / 顯示框的寬度,
其實(shí)微信小程序有swiper組件,使用也是很方便的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
layer插件實(shí)現(xiàn)在彈出層中彈出一警告提示并關(guān)閉彈出層的方法
今天小編就為大家分享一篇layer插件實(shí)現(xiàn)在彈出層中彈出一警告提示并關(guān)閉彈出層的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09php讀取sqlite數(shù)據(jù)庫入門實(shí)例代碼
本文介紹了php讀取sqlite數(shù)據(jù)庫的簡(jiǎn)單例子,php編程中操作sqlite入門實(shí)例,有需要的朋友可以參考下2014-06-06JavaScript判斷一個(gè)URL鏈接是否有效的實(shí)現(xiàn)方法
如何用javascript來判斷請(qǐng)求的url/鏈接有效(可連接,可用)?需要的朋友可以參考下。2011-10-10