您好,登錄后才能下訂單哦!
這篇文章主要介紹小程序如何實現tab和swiper切換效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
小程序實現tab和swiper切換效果展示的具體代碼,具體內容如下
先上效果圖:
實現代碼如下:
wxml頁面
<scroll-view scroll-x="true" class="weui-navbar"> <block wx:for-items="{{tabs}}"> <view id="{{index}}" class="{{activeIndex == index ?'item_on':''}} default-item" bindtap="tabClick" style="width:{{tabW}}px"> {{item}} </view> </block> <view class="weui-navbar-slider" style="transform:translateX({{slideOffset}}px);"></view> </scroll-view> <view class="rec" /> <swiper current="{{activeIndex}}" class="swiper-box" duration="100" bindchange="bindChange"> <swiper-item class="swiper-item"> <view class="slide-view">tab0</view> </swiper-item> <swiper-item class="swiper-item"> <view class="slide-view">tab1</view> </swiper-item> <swiper-item class="swiper-item"> <view class="slide-view">tab2</view> </swiper-item> <swiper-item class="swiper-item"> <view class="slide-view">tab3</view> </swiper-item> <swiper-item class="swiper-item"> <view class="slide-view">tab4</view> </swiper-item> <swiper-item class="swiper-item"> <view class="slide-view">tab5</view> </swiper-item> <swiper-item class="swiper-item"> <view class="slide-view">tab6</view> </swiper-item> </swiper>
wxss樣式:
page { height: 100%; } view , scroll-view{ padding: 0px; margin: 0px; } .weui-navbar{ width: 100%; position: fixed; border-bottom: 1px solid #00bba1; box-sizing: border-box; white-space: nowrap; z-index: 100; background: white; } .rec{ width: 100%; height: 7%; } .weui-navbar .default-item{ /*width:25%;*/ display: inline-block; text-align: center; font-size: 32rpx; box-sizing: border-box; height: 7%; padding-bottom: 20rpx; } .weui-navbar .item_on{ color: #00bba1; } .weui-navbar-slider{ position: absolute; width: 160rpx; height: 2px; content:""; background: #00bba1; bottom: 0px; left: 12.5rpx; transition: transform .3s; } .swiper-box{ height: 900px; border-bottom: 1px solid #d1d1d1; } .swiper-box .swiper-item{ text-align: center; padding-top: 200rpx; height: 100%; }
js代碼:
//index.js //獲取應用實例 var app = getApp(); var mtabW; Page({ data: { tabs:["綜合與繪畫","藝術噴漆","泥塑","紙面繪畫","布面繪畫","中國油畫","水墨畫"], activeIndex:0, slideOffset:0, tabW:0 }, //事件處理函數 onLoad: function () { var that = this; wx.getSystemInfo({ success: function (res) { mtabW = res.windowWidth / 4; //設置tab的寬度 that.setData({ tabW:mtabW }) } }); }, bindViewTap: function() { wx.navigateTo({ url: '../logs/logs' }) }, tabClick:function(e){ var that = this; var idIndex = e.currentTarget.id; var offsetW = e.currentTarget.offsetLeft; //2種方法獲取距離文檔左邊有多少距離 this.setData({ activeIndex:idIndex, slideOffset:offsetW }); }, bindChange:function(e){ var current = e.detail.current; if((current+1)%4 == 0){ } var offsetW = current * mtabW; //2種方法獲取距離文檔左邊有多少距離 this.setData({ activeIndex:current, slideOffset:offsetW }); } })
以上是“小程序如何實現tab和swiper切換效果”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。