wxss
/**index.wxss**/ page{ width: 100%; height: 100%; } .container{ width: 100%; height: 100%; } .nav { height: 80rpx; width: 100%; box-sizing: border-box; overflow: hidden; line-height: 80rpx; background: #f7f7f7; font-size: 16px; white-space: nowrap; position: fixed; top: 0; left: 0; z-index: 99; } .nav-item { width: 20%; display: inline-block; text-align: center; } .nav-item.active{ color: red; } .tab-box{ background: greenyellow; padding-top: 80rpx; height: 100%; box-sizing: border-box; } .tab-content{ overflow-y: scroll; }
wxml
<view class="container"> <!-- tab導(dǎo)航欄 --> <!-- scroll-left屬性可以控制滾動條位置 --> <!-- scroll-with-animation滾動添加動畫過渡 --> <scroll-view scroll-x="true" class="nav" scroll-left="{{navScrollLeft}}" scroll-with-animation="{{true}}"> <block wx:for="{{navData}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx"> <view class="nav-item {{currentTab == idx ?'active':''}}" data-current="{{idx}}" bindtap="switchNav">{{navItem.text}}</view> </block> </scroll-view> <!-- 頁面內(nèi)容 --> <swiper class="tab-box" current="{{currentTab}}" duration="300" bindchange="switchTab"> <swiper-item wx:for="{{[0]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-content"> 1 </swiper-item> <swiper-item wx:for="{{[1]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-content"> 2 </swiper-item> <swiper-item wx:for="{{[2]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-content"> 3 </swiper-item> </swiper> </view>
js
//index.js //獲取應(yīng)用實(shí)例 const app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), navData:[ { text: '首頁' }, { text: '健康' }, { text: '情感' }, { text: '職場' }, { text: '育兒' }, { text: '糾紛' }, { text: '青蔥' }, { text: '上課' }, { text: '下課' } ], currentTab: 0, navScrollLeft: 0 }, //事件處理函數(shù) onLoad: function () { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse) { // 由于 getUserInfo 是網(wǎng)絡(luò)請求庆杜,可能會在 Page.onLoad 之后才返回 // 所以此處加入 callback 以防止這種情況 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在沒有 open-type=getUserInfo 版本的兼容處理 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; //每個tab選項(xiàng)寬度占1/5 var singleNavWidth = this.data.windowWidth / 5; //tab選項(xiàng)居中 this.setData({ navScrollLeft: (cur - 2) * singleNavWidth }) if (this.data.currentTab == cur) { return false; } else { this.setData({ currentTab: cur }) } }, switchTab(event){ var cur = event.detail.current; var singleNavWidth = this.data.windowWidth / 5; this.setData({ currentTab: cur, navScrollLeft: (cur - 2) * singleNavWidth }); } })