微信小程序?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, //對應(yīng)圓球的下標(biāo),css根據(jù)頁面下標(biāo)顯示對應(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組件,使用也是很方便的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)transform實(shí)現(xiàn)扇子效果
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)transform實(shí)現(xiàn)扇子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
小程序自定義tab-bar踩坑實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于小程序自定義tab-bar踩坑實(shí)戰(zhàn)的相關(guān)資料,包括下載代碼、放置文件、修改JS文件、配置app.json和隱藏原生導(dǎo)航欄等步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
OpenLayer實(shí)現(xiàn)自定義坐標(biāo)點(diǎn)的繪制
OpenLayers?是一個(gè)專為Web?GIS?客戶端開發(fā)提供的JavaScript?類庫包,用于實(shí)現(xiàn)標(biāo)準(zhǔn)格式發(fā)布的地圖數(shù)據(jù)訪問。本文將利用OpenLayer實(shí)現(xiàn)自定義坐標(biāo)點(diǎn)的繪制,感興趣的可以了解一下2022-04-04
微信小程序頁面跳轉(zhuǎn)功能之從列表的item項(xiàng)跳轉(zhuǎn)到下一個(gè)頁面的方法
這篇文章主要介紹了微信小程序頁面跳轉(zhuǎn)功能之從列表的item項(xiàng)跳轉(zhuǎn)到下一個(gè)頁面的方法,結(jié)合具體實(shí)例形式總結(jié)分析了微信小程序頁面跳轉(zhuǎn)及列表item項(xiàng)跳轉(zhuǎn)頁面的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
JavaScript解析任意形式的json樹型結(jié)構(gòu)展示
這篇文章主要介紹了JavaScript解析任意形式的json樹型結(jié)構(gòu)展示的相關(guān)資料,需要的朋友可以參考下2017-07-07
微信web端后退強(qiáng)制刷新功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信web端后退強(qiáng)制刷新功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-03-03

