微信小程序?qū)崿F(xiàn)頁面浮動(dòng)導(dǎo)航
一、前言
做復(fù)雜的小程序就與web頁面的區(qū)別原來越小了,一些web頁面的功能會被要求添加到微信小程序頁面中。
二、功能
頁面在滑動(dòng)的時(shí)候頂部頁面導(dǎo)航跟隨滑動(dòng),當(dāng)點(diǎn)擊導(dǎo)航中的任意一項(xiàng)時(shí)返回頁面頂部。
三、實(shí)現(xiàn)
wxml代碼:
<view class='container'>
<view class='navigation {{pageVariable.isFloat == true ? "float-navigation":""}}'>
<view class='{{policyFilter.curSelectNavigationItemFormate(pageVariable.curSelectedItemId,"0")}}' data-id='0' catchtap='selectNavigationItem'>全部</view>
<view class='{{policyFilter.curSelectNavigationItemFormate(pageVariable.curSelectedItemId,"1")}}' data-id='1' catchtap='selectNavigationItem'>保障中</view>
<view class='{{policyFilter.curSelectNavigationItemFormate(pageVariable.curSelectedItemId,"2")}}' data-id='2' catchtap='selectNavigationItem'>已生效</view>
<view class='{{policyFilter.curSelectNavigationItemFormate(pageVariable.curSelectedItemId,"3")}}' data-id='3' catchtap='selectNavigationItem'>未生效</view>
</view>
</view>
wxss代碼:
.navigation { /*導(dǎo)航樣式*/
width: 100%;
display: flex;
justify-content: space-around;
align-items: center;
height: 80rpx;
background-color: #fff;
font-size: 28rpx;
color: #333;
font-weight: 500;
box-shadow: inset 0 0 0 0 rgba(0, 0, 0, 0.30);
}
.float-navigation { /*導(dǎo)航浮動(dòng)起來的css*/
position: fixed;
top: 0;
z-index: 1000;
}
.navigation-item-selected { /*導(dǎo)航項(xiàng)選中的樣式*/
color: #40a0ee;
height: 80rpx;
line-height: 80rpx;
border-bottom: 3rpx solid #40a0ee;
}
js代碼:
Page({
data:function () {
var model = {};
model.pageVariable = {
curSelectedItemId:'0', //頂部導(dǎo)航欄,當(dāng)前選中的項(xiàng)
isFloat:false, //控制導(dǎo)航欄浮動(dòng)
}
return model;
}(),
/**
* 選擇導(dǎo)航
*/
selectNavigationItem:function(e){
this.setData({
'pageVariable.curSelectedItemId': e.currentTarget.dataset.id,
'pageVariable.isFloat':false
});
wx.pageScrollTo({
scrollTop: 0,
});
this.initData(e.currentTarget.dataset.id); //加載數(shù)據(jù)
},
onPageScroll:function(res){
if (res.scrollTop >= 1){ //開始滾動(dòng)
if (!this.data.pageVariable.isFloat){
this.setData({
'pageVariable.isFloat':true
});
}
}else{
this.setData({
'pageVariable.isFloat': false
});
}
}
})
總結(jié):
這個(gè)功能的實(shí)現(xiàn)主要是通過onPageScroll頁面注冊函數(shù)來實(shí)現(xiàn)頁面滾動(dòng),通過pageScrollTo api實(shí)現(xiàn)導(dǎo)航選項(xiàng)在被選中時(shí)返回到頁面頂部。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序仿今日頭條導(dǎo)航欄滾動(dòng)解析
- 微信小程序?qū)隫ant報(bào)錯(cuò)VM292:1 thirdScriptError的解決方法
- 微信小程序用戶授權(quán)彈窗 拒絕時(shí)引導(dǎo)用戶重新授權(quán)實(shí)現(xiàn)
- 微信小程序?qū)崿F(xiàn)的動(dòng)態(tài)設(shè)置導(dǎo)航欄標(biāo)題功能示例
- 微信小程序頂部導(dǎo)航欄滑動(dòng)tab效果
- 微信小程序 配置頂部導(dǎo)航條標(biāo)題顏色的實(shí)現(xiàn)方法
- 微信小程序教程系列之設(shè)置標(biāo)題欄和導(dǎo)航欄(7)
- 微信小程序 導(dǎo)入圖標(biāo)實(shí)現(xiàn)過程詳解
相關(guān)文章
js實(shí)現(xiàn)動(dòng)態(tài)顯示時(shí)間效果
本文主要介紹了js實(shí)現(xiàn)動(dòng)態(tài)顯示時(shí)間效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
JavaScript分步實(shí)現(xiàn)一個(gè)出生日期的正則表達(dá)式
本文把出生日期分割成幾個(gè)部分,分步地介紹了實(shí)現(xiàn)一個(gè)出生日期校驗(yàn)的完整過程。對出生日期正則表達(dá)式感興趣的朋友參考下吧2018-03-03
解決Babylon.js中AudioContext was not allowed&nbs
這篇文章主要介紹了解決Babylon.js中AudioContext was not allowed to start異常問題方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

