微信小程序實現點擊導航條切換頁面
更新時間:2020年11月19日 08:45:25 作者:會唱歌的前端
這篇文章主要為大家詳細介紹了微信小程序實現點擊導航條切換頁面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序實現點擊導航條切換頁面的具體代碼,供大家參考,具體內容如下
我錄制了個gif如下,黃色部分是不可以滑動的,藍色部分可以滑動。

代碼解說:
- 首先我在js自定義了navState參數用于判斷導航的當前狀態(tài),
- 定義了data-index用于js中動態(tài)修改導航的當前狀態(tài),
- nav-switch-style為選擇導航條時的樣式,
- 不可滑動視圖切換很簡單,用wx:if判斷狀態(tài)顯示相應頁就好了,
- 滑動頁視圖切換要用到swiper和 swiper-item,
- 用bindchang方法監(jiān)聽滑塊,current 改變時會觸發(fā) change 事件(還有個bindanimationfinish方法監(jiān)聽也是可以用的,詳細請看官方文檔)
- 動態(tài)的綁定了current滑塊的index,這樣就可以實現點擊導航條滑塊跟著滾動,
- 相反的,當滑動滑塊時,就可以根據current的值來動態(tài)修改導航的狀態(tài)了。
wxml代碼:
<!-- 導航條 -->
<view class="nav">
<view bindtap="navSwitch" data-index="0" class="{{navState==0 ? 'nav-switch-style':''}}">頁面一</view>
<view bindtap="navSwitch" data-index="1" class="{{navState==1 ? 'nav-switch-style':''}}">頁面二</view>
<view bindtap="navSwitch" data-index="2" class="{{navState==2 ? 'nav-switch-style':''}}">頁面三</view>
</view>
<!-- 不可滑動頁 -->
<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>
<!-- 滑動頁 -->
<swiper bindchange="bindchange" current="{{navState}}">
<block>
<swiper-item>
<view class="style-roll">
<text>左右可滑動1</text>
</view>
</swiper-item>
<swiper-item>
<view class="style-roll">
<text>左右可滑動2</text>
</view>
</swiper-item>
<swiper-item>
<view class="style-roll">
<text>左右可滑動3</text>
</view>
</swiper-item>
</block>
</swiper>
js代碼:
Page({
data: {
navState: 0,//導航狀態(tài)
},
//監(jiān)聽滑塊
bindchange(e) {
// console.log(e.detail.current)
let index = e.detail.current;
this.setData({
navState:index
})
},
//點擊導航
navSwitch: function(e) {
// console.log(e.currentTarget.dataset.index)
let index = e.currentTarget.dataset.index;
this.setData({
navState:index
})
},
/**
* 生命周期函數--監(jiān)聽頁面加載
*/
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;
}
為大家推薦現在關注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
在Javascript操作JSON對象,增加 刪除 修改的簡單實現
下面小編就為大家?guī)硪黄贘avascript操作JSON對象,增加 刪除 修改的簡單實現。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
javascript中typeof操作符和constucor屬性檢測
這篇文章主要介紹了javascript中typeof操作符和constucor屬性檢測的相關資料,需要的朋友可以參考下2015-02-02
使用mock.js隨機數據和使用express輸出json接口的實現方法
這篇文章主要介紹了使用mock.js隨機數據和使用express輸出json接口的實現方法,需要的朋友可以參考下2018-01-01

