您好,登錄后才能下訂單哦!
本文實例為大家分享了微信小程序實現tab左右切換展示的具體代碼,供大家參考,具體內容如下
分析
1、設置data-current屬性用于:點擊當前項時,通過點擊事件swichNav中處理e.dataset.current取到點擊的目標值。
2、swiper組件的current組件用于控制當前顯示哪一頁
3、swiper組件綁定change事件switchTab,通過e.detail.current拿到當前頁
wxml:
<view class="record-box"> <view class="nav"> <scroll-view scroll-x="true" class="navbar-box" srcoll-left="{{navScrollLeft}}" srcoll-width-animation="{{true}}"> <block wx:for="{{recordMain}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx"> <view class="nav-item {{currentTab == idx ? 'text' : ''}}" data-current="{{idx}}" bindtap="switchNav"> <label>{{navItem.text}}</label> </view> </block> </scroll-view> </view> </view> ------------------------------------------------ <view class="record-ft"> <swiper class="tab-box" current="{{currentTab}}" duration="300" bindchange="switchTab"> <swiper-item wx:for="{{[0,1,2,3]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-cnetent"> <block wx:for="{{tabContent}}" wx:for-item="tabItem" wx:key=" "> <view class="padding-box" bindtap="navigateUrl" data-url="/pages/index/index"> <view class="weui-list-box weui-main"> <view class="weui-list-bd"> <view class="img-box"> <image src="{{tabItem}}" class="img" mode="widthFix"></image> </view> </view> <view class="weui-list-ft"> <view class="weui-title"> <view class="text-flow clamp-xs font-sm">{{tabItem.title}}</view> <view class="text"> <laber>{{tabItem.text}}</laber> <laber>{{tabItem.text}}</laber> </view> </view> <view class="weui-cost"> <view>¥<laber>{{tanItem.cost}}</laber></view> </view> </view> </view> </view> </block> </swiper-item> </swiper> </view>
JS
Page ({ const app = getApp() data: { recordMian: [ { title: "插畫藝術" }, { title: "工藝作品" }, { title: "服裝藝術" }, { title: "三維建模" }, ], tabContent: [ { title: "臺燈臥室床頭 簡約現代書房 北歐宜家創意裝飾個性圓球臺燈床頭燈", text: "臺燈", cost: "1255", imgUrl: "http://img.weiye.me/zcimgdir/album/file_59b8f7b66ba7f.jpg", }, { title: "雪域冰雪天地8寸牛乳芝士蛋糕", text: "蛋糕", cost: "15", imgUrl: "http://img.weiye.me/zcimgdir/album/file_59b8f7b66ba7f.jpg", }, ], currentTab: 0, navScrollLeft: 0 }, // 事件處理函數 onLoad: function() { // 控制record-box隨鼠標切換調整位置 if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse) { app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } wx.getSystemInfo({ success: (res) => { this.setData({ pixelRatio: res.pixelRatio, windowHeight: res.windowHeight, windowWidth: res.windowWidth }) } }) }, // 滑動事件 // 點擊標題切換當前頁時改變樣式 switchNav(event) { var cur = event.currentTarget.dataset.current; if (this.data.currentTab == cur) { return false; } else { this.setData({ currentTab: cur }) } }, // 滾動切換標簽樣式 switchTab(event) { var cur = evnet.detail.current; var singeNavWidth = this.data.windowWidth / 5; this.setData({ currentTab: cur, navScrollLeft: (cur - 2) * singleNavWidth }); } })
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。