当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现页面浮动导航

微信小程序实现页面浮动导航

2019年03月17日  | 移动技术网IT编程  | 我要评论

一、前言

做复杂的小程序就与web页面的区别原来越小了,一些web页面的功能会被要求添加到微信小程序页面中。

二、功能

页面在滑动的时候顶部页面导航跟随滑动,当点击导航中的任意一项时返回页面顶部。

三、实现

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 { /*导航样式*/
 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 { /*导航浮动起来的css*/
 position: fixed;
 top: 0;
 z-index: 1000;
}
 
.navigation-item-selected { /*导航项选中的样式*/
 color: #40a0ee;
 height: 80rpx;
 line-height: 80rpx;
 border-bottom: 3rpx solid #40a0ee;
}

js代码:

page({
 data:function () {
 var model = {};
 model.pagevariable = {
  curselecteditemid:'0', //顶部导航栏,当前选中的项
  isfloat:false, //控制导航栏浮动
 }
 return model;
 }(),
 /**
 * 选择导航
 */
 selectnavigationitem:function(e){
 this.setdata({
  'pagevariable.curselecteditemid': e.currenttarget.dataset.id,
  'pagevariable.isfloat':false
 });
 wx.pagescrollto({
  scrolltop: 0,
 });
 this.initdata(e.currenttarget.dataset.id); //加载数据
 },
 onpagescroll:function(res){
 if (res.scrolltop >= 1){ //开始滚动
  if (!this.data.pagevariable.isfloat){
  this.setdata({
   'pagevariable.isfloat':true
  });
  }
 }else{
  this.setdata({
  'pagevariable.isfloat': false
  });
 }
 }
})

总结:

这个功能的实现主要是通过onpagescroll页面注册函数来实现页面滚动,通过pagescrollto api实现导航选项在被选中时返回到页面顶部。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网