微信小程序tab左右滑動(dòng)切換功能的實(shí)現(xiàn)代碼
效果圖:

一、簡(jiǎn)介
自己的小程序需要實(shí)現(xiàn)這樣的功能
1.核心思想
swiper 和scroll-view共用兩個(gè)變量currentTab navScrollLeft,當(dāng)點(diǎn)擊nav或者滑動(dòng)swiper時(shí)設(shè)置兩個(gè)變量的值為當(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屬性可以控制滾動(dòng)條位置 -->
<!-- scroll-with-animation滾動(dòng)添加動(dòng)畫過渡 -->
<!--
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":滑動(dòng)動(dòng)畫時(shí)長(zhǎng) -->
<!-- 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;
//每個(gè)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%;
/* 假如您需要并排放置兩個(gè)帶邊框的框,
可通過將 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.fzitv.net/article/169290.htm
解決過程
1.tab的寬度固定為1/5,可以改進(jìn)成根據(jù)tab的內(nèi)容變化
四、優(yōu)化
0.效果圖

1.每個(gè)tab長(zhǎng)度自適應(yīng) 2.先前隔tab點(diǎn)擊時(shí)
如果當(dāng)前處于1,點(diǎn)擊3時(shí),路徑時(shí)1-2-3,真機(jī)測(cè)試后,會(huì)直接跳轉(zhuǎn)3,不會(huì)影響體驗(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({
// 每一個(gè)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("橫向滾動(dòng)條位置", this.data.scrollLeft);
},
switchTab(e) {
console.log(e);
var cur = e.detail.current;
this.setData({
TabCur: cur,
scrollLeft: cur * 60,
});
}
到此這篇關(guān)于微信小程序tab左右滑動(dòng)切換功能的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)小程序tab滑動(dòng)切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 微信小程序下面商品左右滑動(dòng)上面tab也跟隨變動(dòng)功能實(shí)現(xiàn)
- 微信小程序tab切換可滑動(dòng)切換導(dǎo)航欄跟隨滾動(dòng)實(shí)現(xiàn)代碼
- 微信小程序自定義可滑動(dòng)頂部TabBar選項(xiàng)卡實(shí)現(xiàn)頁面切換功能示例
- 微信小程序頂部導(dǎo)航欄滑動(dòng)tab效果
- 10行代碼實(shí)現(xiàn)微信小程序滑動(dòng)tab切換
- 微信小程序滾動(dòng)Tab實(shí)現(xiàn)左右可滑動(dòng)切換
- 微信小程序開發(fā)之實(shí)現(xiàn)選項(xiàng)卡(窗口頂部TabBar)頁面切換
- 微信小程序?qū)崿F(xiàn)tab左右切換效果
- 微信小程序 Tab頁切換更新數(shù)據(jù)
- 微信小程序自定義可滑動(dòng)的tab切換
相關(guān)文章
js判斷iframe中元素是否存在的實(shí)現(xiàn)代碼
這篇文章主要介紹了js判斷iframe中元素是否存在的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-12-12
javascript parseUrl函數(shù)(來自國(guó)外的獲取網(wǎng)址url參數(shù))
在外國(guó)一博客看到一個(gè)很好的函數(shù),獲取網(wǎng)址url等地址參數(shù)。非常不錯(cuò),值得參考與收藏。2010-06-06
javascript實(shí)現(xiàn)圖片輪換動(dòng)作方法
這篇文章主要介紹了javascript實(shí)現(xiàn)圖片輪換動(dòng)作方法,文章通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
js基礎(chǔ)之DOM中元素對(duì)象的屬性方法詳解
下面小編就為大家?guī)硪黄猨s基礎(chǔ)之DOM中元素對(duì)象的屬性方法詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
使用typescript改造koa開發(fā)框架的實(shí)現(xiàn)
這篇文章主要介紹了使用typescript改造koa開發(fā)框架的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

