微信小程序之 swiper自定義dot
微信自帶的swiper不支持dot樣式修改,如果UI設計的樣式有變化,將很難自定義
以下,提供一種方案來解決dot樣式自定義的問題
如圖:
大致思路 就是隱藏默認swiper的dots,然后自定義自己的dotview,并同步事件
<view class="swiper-container"> <swiper autoplay="{{autoplay}}" circular="{{true}}" indicator-dots="{{false}}" current="{{swiperCurrent}}" vertical="{{false}}" bindchange="swiperChange"> <block wx:for="{{imgUrls}}"> <swiper-item> <image src="{{item}}" mode="widthFix"></image> </swiper-item> </block> </swiper> <view wx:if="{{imgUrls.length > 1}}" class="dots-container"> <block wx:for="{{imgUrls}}"> <view class="dot{{index == swiperCurrent ? ' active' : ''}}" bind:tap="onDotTap" mark:index="{{index}}"></view> </block> </view> </view>
Page({ data: { autoplay:true, swiperCurrent: 0, imgUrls: [ '','','','' ] }, swiperChange: function (e) { let that = this; that.setData({ swiperCurrent: e.detail.current }) }, onDotTap(e){ this.setData({autoplay:false}); console.log(e); const {index} = e.mark; //設置滾動 this.setData({swiperCurrent:index,autoplay:true}); } })
.swiper-container{ position: relative; } swiper{ height: 300rpx; background-color: orange; } swiper-item{ height: 100%; width: 100%; background-color: blue; } swiper-item image{ width: 100%; height: 100%; } swiper-item:nth-child(1){ background-color: chartreuse; } swiper-item:nth-child(2){ background-color: red; } swiper-item:nth-child(3){ background-color: rebeccapurple; } swiper-item:nth-child(4){ background-color: cyan; } .dots-container{ position: absolute; display: flex; justify-content: center; align-items: center; bottom: 10rpx; left:50%; transform:translate(-50%); /* background-color: gray; */ } .dot{ height: 20rpx; width: 20rpx; background-color: #9A9A9A; border-radius: 10rpx; margin-left: 20rpx; transition: all .25s; box-sizing: border-box; border:1rpx solid white; } .dot.active{ width: 40rpx; background: #fff; } .dot:nth-of-type(1){ margin-left: 0; }
如果您的問題還未解決可以聯系站長付費協助。

有問題可以加入技術QQ群一起交流學習
本站vip會員 請加入無憂模板網 VIP群(50604020) PS:加入時備注用戶名或昵稱
普通注冊會員或訪客 請加入無憂模板網 技術交流群(50604130)
客服微信號:15898888535
聲明:本站所有文章資源內容,如無特殊說明或標注,均為采集網絡資源。如若內容侵犯了原著者的合法權益,可聯系站長刪除。