微信小程序tab左右滑動切換功能的實(shí)現(xiàn)代碼
效果圖:
一、簡介
自己的小程序需要實(shí)現(xiàn)這樣的功能
1.核心思想
swiper 和scroll-view共用兩個變量currentTab navScrollLeft,當(dāng)點(diǎn)擊nav或者滑動swiper時設(shè)置兩個變量的值為當(dāng)前的index
二、實(shí)現(xiàn)
tab導(dǎo)航欄使用<scroll-view>標(biāo)簽,內(nèi)容使用<swiper>
1.wxml實(shí)現(xiàn)
<view class="container"> <!-- tab導(dǎo)航欄 --> <!-- scroll-left屬性可以控制滾動條位置 --> <!-- scroll-with-animation滾動添加動畫過渡 --> <!-- scroll-x="true" navScrollLeft: 0初值 navData:tab text 使用 wx:for-item 可以指定數(shù)組當(dāng)前元素的變量名, 使用 wx:for-index 可以指定數(shù)組當(dāng)前下標(biāo)的變量名: --> <!--tabs --> <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"> <!-- 判斷是否選中,選中設(shè)置樣式 --> <!-- switchNav --> <view class="nav-item {{currentTab == idx ?'active':''}}" data-current="{{idx}}" bindtap="switchNav"> {{navItem.text}}</view> </block> </scroll-view> <!-- 頁面內(nèi)容 --> <!-- duration="300":滑動動畫時長 --> <!-- switchTab --> <swiper class="tab-box" current="{{currentTab}}" duration="300" bindchange="switchTab"> <swiper-item wx:for="{{[0,1,2,3,4,5,6]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-content"> {{tabItem}} </swiper-item> </swiper> </view>
2.js實(shí)現(xiàn)
//index.js //獲取應(yīng)用實(shí)例 const app = getApp() Page({ data: { navData:[ { text: '新聞' }, { text: '表白' }, { text: '外賣' }, { text: '當(dāng)家教' }, { text: '找家教' }, { text: '租房子' }, { text: '駕校' } ], currentTab: 0, navScrollLeft: 0 }, //事件處理函數(shù) onLoad: function () { }, switchNav(event){ // 獲取當(dāng)前tab 的id 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 }) // 判斷id是否和點(diǎn)擊的tab id 一致 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 }); } })
3.wxss實(shí)現(xiàn)
/**index.wxss**/ page { width: 100%; height: 100%; } .container { width: 100%; height: 100%; } .nav { /* 設(shè)置tab-nav寬高度 */ height: 80rpx; width: 100%; /* 假如您需要并排放置兩個帶邊框的框, 可通過將 box-sizing 設(shè)置為 "border-box"。 */ box-sizing: border-box; overflow: hidden; /* 居中 */ line-height: 80rpx; background: #f7f7f7; font-size: 16px; /* 規(guī)定段落中的文本不進(jìn)行換行: */ 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: green; } .tab-box { background: rgb(31, 201, 96); /* 這里設(shè)置成nav的高度 */ padding-top: 80rpx; height: 100%; box-sizing: border-box; } .tab-content { /* 裁剪 div 元素中內(nèi)容的左/右邊緣 - 如果溢出元素的內(nèi)容區(qū)域的話: */ overflow-y: scroll; }
三、參考和總結(jié)
此文章參考 http://www.dbjr.com.cn/article/169290.htm
解決過程
1.tab的寬度固定為1/5,可以改進(jìn)成根據(jù)tab的內(nèi)容變化
四、優(yōu)化
0.效果圖
1.每個tab長度自適應(yīng) 2.先前隔tab點(diǎn)擊時
如果當(dāng)前處于1,點(diǎn)擊3時,路徑時1-2-3,真機(jī)測試后,會直接跳轉(zhuǎn)3,不會影響體驗(yàn)
// *******************************導(dǎo)航欄選擇獲取id和導(dǎo)航欄的位置************************************** tabSelect(e) { console.log("結(jié)果:", e); // 操作新聞數(shù)據(jù)庫 var cur = e.currentTarget.dataset.id; //tab選項(xiàng)居中 this.setData({ // 每一個tab的id TabCur: e.currentTarget.dataset.id, //自適應(yīng) scrollLeft: (e.currentTarget.dataset.id) * 60, }) // 判斷id是否和點(diǎn)擊的tab id 一致 if (this.data.currentTab == cur) { return false; } else { this.setData({ currentTab: cur }) } console.log(e.currentTarget.dataset.id); console.log(this.data.TabCur); console.log("橫向滾動條位置", this.data.scrollLeft); }, switchTab(e) { console.log(e); var cur = e.detail.current; this.setData({ TabCur: cur, scrollLeft: cur * 60, }); }
到此這篇關(guān)于微信小程序tab左右滑動切換功能的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)小程序tab滑動切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 微信小程序下面商品左右滑動上面tab也跟隨變動功能實(shí)現(xiàn)
- 微信小程序tab切換可滑動切換導(dǎo)航欄跟隨滾動實(shí)現(xiàn)代碼
- 微信小程序自定義可滑動頂部TabBar選項(xiàng)卡實(shí)現(xiàn)頁面切換功能示例
- 微信小程序頂部導(dǎo)航欄滑動tab效果
- 10行代碼實(shí)現(xiàn)微信小程序滑動tab切換
- 微信小程序滾動Tab實(shí)現(xiàn)左右可滑動切換
- 微信小程序開發(fā)之實(shí)現(xiàn)選項(xiàng)卡(窗口頂部TabBar)頁面切換
- 微信小程序?qū)崿F(xiàn)tab左右切換效果
- 微信小程序 Tab頁切換更新數(shù)據(jù)
- 微信小程序自定義可滑動的tab切換
相關(guān)文章
bootstrap導(dǎo)航欄、下拉菜單、表單的簡單應(yīng)用實(shí)例解析
這篇文章主要介紹了bootstrap導(dǎo)航欄、下拉菜單、表單的簡單應(yīng)用實(shí)例解析,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-01-01layui實(shí)現(xiàn)table加載的示例代碼
今天小編就為大家分享一篇layui實(shí)現(xiàn)table加載的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08分離與繼承的思想實(shí)現(xiàn)圖片上傳后的預(yù)覽功能:ImageUploadView
本文要介紹的是網(wǎng)頁中常見的圖片上傳后直接在頁面生成小圖預(yù)覽的實(shí)現(xiàn)思路,考慮到該功能有一定的適用性,于是把相關(guān)的邏輯封裝成了一個ImageUploadView組件,實(shí)際使用效果可查看下一段的git效果圖2016-04-04統(tǒng)計(jì)出現(xiàn)最多的字符次數(shù)的js代碼
一小段代碼,經(jīng)常出現(xiàn)在面試筆試題中的:統(tǒng)計(jì)一個字符串中出現(xiàn)最多的字符的次數(shù),可以是英文或者數(shù)字。2010-12-12