欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序tab切換可滑動(dòng)切換導(dǎo)航欄跟隨滾動(dòng)實(shí)現(xiàn)代碼

 更新時(shí)間:2019年09月04日 13:24:42   作者:竹林中  
這篇文章主要介紹了微信小程序tab切換可滑動(dòng)切換導(dǎo)航欄跟隨滾動(dòng)實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

簡(jiǎn)介

看到今日頭條小程序頁(yè)面可以滑動(dòng)切換,而且tab導(dǎo)航條也會(huì)跟著滾動(dòng),點(diǎn)擊tab導(dǎo)航,頁(yè)面滑動(dòng),切導(dǎo)航欄也會(huì)跟著滾動(dòng),就想著要怎么實(shí)現(xiàn)這個(gè)功能

像商城類(lèi)商品類(lèi)目如果做成左右滑動(dòng)切換類(lèi)目用戶(hù)體驗(yàn)應(yīng)該會(huì)好很多,我這里只是一個(gè)小demo,沒(méi)有怎么去考慮數(shù)據(jù)的問(wèn)題,主要是想著去實(shí)現(xiàn)這么個(gè)功能,有可能后期引入數(shù)據(jù)后會(huì)出現(xiàn)問(wèn)題,歡迎提出互相討論

解決過(guò)程

1.在想要實(shí)現(xiàn)這個(gè)問(wèn)題的時(shí)候找了不少別人的博客看,主體頁(yè)面布局方面是比較統(tǒng)一的,tab導(dǎo)航欄使用<scroll-view>標(biāo)簽,內(nèi)容使用<swiper>,其中的使用方法和參數(shù)希望自行參考api文檔,不過(guò)多解釋

布局代碼如下:

wxml

<view class="container">
 <!-- tab導(dǎo)航欄 -->
 <!-- scroll-left屬性可以控制滾動(dòng)條位置 -->
 <!-- scroll-with-animation滾動(dòng)添加動(dòng)畫(huà)過(guò)渡 -->
 <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>
 <!-- 頁(yè)面內(nèi)容 -->
 <swiper class="tab-box" current="{{currentTab}}" duration="300" bindchange="switchTab">  
  <swiper-item wx:for="{{[0,1,2,3,4,5,6,7,8]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-content">
   {{tabItem}}
  </swiper-item>
 </swiper>
</view>

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;
}

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: '首頁(yè)'
   },
   {
    text: '健康'
   },
   {
    text: '情感'
   },
   {
    text: '職場(chǎng)'
   },
   {
    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ò)請(qǐng)求,可能會(huì)在 Page.onLoad 之后才返回
   // 所以此處加入 callback 以防止這種情況
   app.userInfoReadyCallback = res => {
    this.setData({
     userInfo: res.userInfo,
     hasUserInfo: true
    })
   }
  } else {
   // 在沒(méi)有 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; 
  //每個(gè)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
  });
 }
})

頁(yè)面代碼如上面三部分,可以直接新建一項(xiàng)目進(jìn)行測(cè)試

效果圖如下

注意事項(xiàng)

在處理頂部tab導(dǎo)航跟隨底部頁(yè)面滑動(dòng)的時(shí)候遇到一個(gè)問(wèn)題,就是在給<scroll-view>中的scrll-left賦值的時(shí)候遇到的問(wèn)題

邏輯上講初始時(shí)tab導(dǎo)航下標(biāo)小于2時(shí)導(dǎo)航欄不滾動(dòng),當(dāng)大于2時(shí)向左滑動(dòng),以使被選中的導(dǎo)航欄居中,但是當(dāng)最左側(cè)的選項(xiàng)因?yàn)樽蠡床坏胶?,我又點(diǎn)擊左側(cè)選項(xiàng)希望導(dǎo)航往右滑動(dòng),能夠看到左邊的導(dǎo)航,按上面的js代碼計(jì)算scroll-left會(huì)產(chǎn)生負(fù)值,但是scroll-left即使為負(fù)值,但是滾動(dòng)條不會(huì)向左縮進(jìn)去,所以即使為負(fù)值,相當(dāng)于為0,當(dāng)時(shí)做的時(shí)候一直在思考這個(gè)怎么用邏輯解決,想著要寫(xiě)各種判斷,計(jì)算距離,結(jié)果到最后一句代碼直接賦值就搞定了,也是很無(wú)語(yǔ)。

總結(jié)

以上所述是小編給大家介紹的微信小程序tab切換可滑動(dòng)切換導(dǎo)航欄跟隨滾動(dòng)實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論