微信小程序?qū)崿F(xiàn)滴滴導(dǎo)航tab切換效果
本文實例為大家分享了微信小程序?qū)崿F(xiàn)tab切換效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖如下: (請自動忽視底部tab.....)

簡單介紹一下:頂部導(dǎo)航使用 scroll-view 組件 中間的內(nèi)容部分使用 swiper 組件
實現(xiàn)的邏輯就是: 先這樣在這樣,這樣然后那樣。(此處省略200個字)。
代碼如下,復(fù)制可用
wxml
<view class="contain">
<!-- 導(dǎo)航欄 -->
<scroll-view class="tab" scroll-x scroll-left="{{tabScroll}}" scroll-with-animation="true">
<block wx:for="{{menuList}}" wx:key="index">
<view class="tab-item {{currentTab == index ? 'active' : ''}}" data-current="{{index}}" bindtap='clickMenu'>{{item.name}}</view>
</block>
</scroll-view>
<!-- 頁面 -->
<swiper class="content" style='height: {{height}}px' duration="1000" current="{{currentTab}}" bindchange="changeContent">
<swiper-item class="content-item" wx:for="{{menuList}}" wx:key="index">這里是{{item.name}}</swiper-item>
</swiper>
</view>
js
Page({
data: {
menuList: [{
name: "快車"
}, {
name: "順風車"
}, {
name: "外賣"
}, {
name: "單車"
}, {
name: "禮橙專車"
}, {
name: "出租車"
}, {
name: "公交"
}, {
name: "代駕"
}, {
name: "豪華車"
}, {
name: "自駕租車"
}, {
name: "拼車"
}, {
name: "二手車"
}],
tabScroll: 0,
currentTab: 0,
windowHeight: '',
windowWidth: ''
},
onLoad: function() {
wx.getSystemInfo({ // 獲取當前設(shè)備的寬高,文檔有
success: (res) => {
this.setData({
windowHeight: res.windowHeight,
windowWidth: res.windowWidth
})
},
})
},
onReady: function() {
wx.setNavigationBarTitle({ //修改標題文字
title: ''
})
},
clickMenu: function(e) {
var current = e.currentTarget.dataset.current //獲取當前tab的index
var tabWidth = this.data.windowWidth / 5 // 導(dǎo)航tab共5個,獲取一個的寬度
this.setData({
tabScroll: (current - 2) * tabWidth //使點擊的tab始終在居中位置
})
if (this.data.currentTab == current) {
return false
} else {
this.setData({currentTab: current })
}
},
changeContent: function(e) {
var current = e.detail.current // 獲取當前內(nèi)容所在index,文檔有
var tabWidth = this.data.windowWidth / 5
this.setData({
currentTab: current,
tabScroll: (current - 2) * tabWidth
})
}
})
css
.contain{
width: 100%;
height: 100%;
}
.tab{
width: 100%;
height: 100rpx;
position: fixed;
top: 0;
left: 0;
z-index: 100;
white-space: nowrap;
box-sizing: border-box;
overflow: hidden;
line-height: 100rpx;
}
.tab-item{
display: inline-block;
width: 20%;
text-align: center;
font-size: 14px;
color: #8f9193;
}
.active{
color: #ff502c
}
.content{
padding-top: 100rpx;
box-sizing: border-box;
text-align: center;
font-size: 14px;
}
.content-item{
overflow-y: scroll
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
新年快樂! javascript實現(xiàn)超級炫酷的3D煙花特效
這篇文章主要為大家詳細介紹了javascript實現(xiàn)超級炫酷的3D煙花特效,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
JS根據(jù)生日月份和日期計算星座的簡單實現(xiàn)方法
這篇文章主要介紹了JS根據(jù)生日月份和日期計算星座的簡單實現(xiàn)方法,涉及javascript日期時間與字符串操作相關(guān)技巧,需要的朋友可以參考下2016-11-11
JavaScript判斷數(shù)組的方法總結(jié)與推薦
這篇文章主要給大家介紹了關(guān)于JavaScript判斷數(shù)組方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下2022-02-02
JavaScript實現(xiàn)給浮點數(shù)添加千分位逗號的多種方法
JavaScript 是一門強大的前端語言,在處理數(shù)值格式化時提供了多種方法,在開發(fā)過程中,我們經(jīng)常需要將大數(shù)字格式化,使其更具可讀性,例如,將 12000000.11 轉(zhuǎn)換為 12,000,000.11,本文將詳細介紹 JavaScript 中如何實現(xiàn)這種格式化,需要的朋友可以參考下2025-04-04
javascript實現(xiàn)鼠標移到Image上方時顯示文字效果的方法
這篇文章主要介紹了javascript實現(xiàn)鼠標移到Image上方時顯示文字效果的方法,涉及javascript鼠標事件及圖文屬性動態(tài)設(shè)置的相關(guān)技巧,可用于為圖片增加文字提示效果,需要的朋友可以參考下2015-08-08

