微信小程序?qū)崿F(xiàn)點(diǎn)擊導(dǎo)航條切換頁(yè)面
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)點(diǎn)擊導(dǎo)航條切換頁(yè)面的具體代碼,供大家參考,具體內(nèi)容如下
我錄制了個(gè)gif如下,黃色部分是不可以滑動(dòng)的,藍(lán)色部分可以滑動(dòng)。

代碼解說(shuō):
- 首先我在js自定義了navState參數(shù)用于判斷導(dǎo)航的當(dāng)前狀態(tài),
- 定義了data-index用于js中動(dòng)態(tài)修改導(dǎo)航的當(dāng)前狀態(tài),
- nav-switch-style為選擇導(dǎo)航條時(shí)的樣式,
- 不可滑動(dòng)視圖切換很簡(jiǎn)單,用wx:if判斷狀態(tài)顯示相應(yīng)頁(yè)就好了,
- 滑動(dòng)頁(yè)視圖切換要用到swiper和 swiper-item,
- 用bindchang方法監(jiān)聽滑塊,current 改變時(shí)會(huì)觸發(fā) change 事件(還有個(gè)bindanimationfinish方法監(jiān)聽也是可以用的,詳細(xì)請(qǐng)看官方文檔)
- 動(dòng)態(tài)的綁定了current滑塊的index,這樣就可以實(shí)現(xiàn)點(diǎn)擊導(dǎo)航條滑塊跟著滾動(dòng),
- 相反的,當(dāng)滑動(dòng)滑塊時(shí),就可以根據(jù)current的值來(lái)動(dòng)態(tài)修改導(dǎo)航的狀態(tài)了。
wxml代碼:
<!-- 導(dǎo)航條 -->
<view class="nav">
<view bindtap="navSwitch" data-index="0" class="{{navState==0 ? 'nav-switch-style':''}}">頁(yè)面一</view>
<view bindtap="navSwitch" data-index="1" class="{{navState==1 ? 'nav-switch-style':''}}">頁(yè)面二</view>
<view bindtap="navSwitch" data-index="2" class="{{navState==2 ? 'nav-switch-style':''}}">頁(yè)面三</view>
</view>
<!-- 不可滑動(dòng)頁(yè) -->
<view>
<view wx:if="{{navState==0}}" class="style-default">1</view>
<view wx:elif="{{navState==1}}" class="style-default">2</view>
<view wx:else="{{navState==2}}" class="style-default">3</view>
</view>
<!-- 滑動(dòng)頁(yè) -->
<swiper bindchange="bindchange" current="{{navState}}">
<block>
<swiper-item>
<view class="style-roll">
<text>左右可滑動(dòng)1</text>
</view>
</swiper-item>
<swiper-item>
<view class="style-roll">
<text>左右可滑動(dòng)2</text>
</view>
</swiper-item>
<swiper-item>
<view class="style-roll">
<text>左右可滑動(dòng)3</text>
</view>
</swiper-item>
</block>
</swiper>
js代碼:
Page({
data: {
navState: 0,//導(dǎo)航狀態(tài)
},
//監(jiān)聽滑塊
bindchange(e) {
// console.log(e.detail.current)
let index = e.detail.current;
this.setData({
navState:index
})
},
//點(diǎn)擊導(dǎo)航
navSwitch: function(e) {
// console.log(e.currentTarget.dataset.index)
let index = e.currentTarget.dataset.index;
this.setData({
navState:index
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面加載
*/
onLoad: function(options) {
},
})
wxss代碼:
.nav{
display: flex;
justify-content: space-around;
padding: 20rpx;
background-color: rgb(129, 241, 55);
font-size: 30rpx;
}
.nav-switch-style{
color: snow;
}
.style-default{
background-color: rgb(247, 229, 130);
padding: 100rpx 0;
text-align: center;
}
.style-roll{
background-color: rgb(130, 177, 247);
padding: 100rpx 0;
text-align: center;
}
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序點(diǎn)擊按鈕動(dòng)態(tài)切換input的disabled禁用/啟用狀態(tài)功能
- 微信小程序點(diǎn)擊頂部導(dǎo)航欄切換樣式代碼實(shí)例
- 微信小程序 點(diǎn)擊切換樣式scroll-view實(shí)現(xiàn)代碼實(shí)例
- 微信小程序開發(fā)之實(shí)現(xiàn)選項(xiàng)卡(窗口頂部TabBar)頁(yè)面切換
- 微信小程序?qū)崿F(xiàn)tab左右切換效果
- 微信小程序 Tab頁(yè)切換更新數(shù)據(jù)
- 微信小程序 swiper制作tab切換實(shí)現(xiàn)附源碼
- 微信小程序開發(fā)之選項(xiàng)卡(窗口底部TabBar)頁(yè)面切換
- 微信小程序?qū)崿F(xiàn)tab切換效果
- 微信小程序?qū)崿F(xiàn)tab點(diǎn)擊切換
相關(guān)文章
在Javascript操作JSON對(duì)象,增加 刪除 修改的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇在Javascript操作JSON對(duì)象,增加 刪除 修改的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
JS實(shí)現(xiàn)兩周內(nèi)自動(dòng)登錄功能
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)兩周內(nèi)自動(dòng)登錄功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
javascript中typeof操作符和constucor屬性檢測(cè)
這篇文章主要介紹了javascript中typeof操作符和constucor屬性檢測(cè)的相關(guān)資料,需要的朋友可以參考下2015-02-02
使用mock.js隨機(jī)數(shù)據(jù)和使用express輸出json接口的實(shí)現(xiàn)方法
這篇文章主要介紹了使用mock.js隨機(jī)數(shù)據(jù)和使用express輸出json接口的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-01-01
Bootstrap table 定制提示語(yǔ)的加載過(guò)程
bootstrap-table是在bootstrap-table的基礎(chǔ)上寫出來(lái)的,專門用于顯示數(shù)據(jù)的表格插件。這篇文章主要介紹了Bootstrap table 定制提示語(yǔ),需要的朋友可以參考下2017-02-02
javascript中Date對(duì)象的使用總結(jié)
本文介紹了Date對(duì)象的使用方法,清晰明了,實(shí)現(xiàn)方法有多種,僅供大家參考,希望對(duì)大家有所幫助,下面就跟小編一起來(lái)看看吧2016-11-11

