最近开发小程序的时候遇到这样一个需求:如图1

页面向下滚动到白色导航的位置时,白色导航固定到页面最上方;当页面向上滚动到白色导航时,白色导航恢复到原始位置;点击各个导航,平滑的跳到相应位置。

思路1:

1.给导航设置position: absolute; 页面向下滚动到白色导航的位置时,将给导航设置为position: fixed;

2.绑定小程序滚动事件bindscroll,监听滚动距离;

代码如下:

wxml:

<scroll-view class="layout" bindscroll='layoutScroll' scroll-y="true" >
<view class='banner'>
<text>我是一个banner</text>
</view>
<!-- 导航开始 -->
<view class='nav clearfix {{navFixed?"positionFixed":""}}'>
<view class='nav_row'>
<text>导航1</text>
</view>
<view class='nav_row'>
<text>导航2</text>
</view>
<view class='nav_row'>
<text>导航3</text>
</view>
<view class='nav_row'>
<text>导航4</text>
</view>
</view>
<!-- 导航结束 -->
<!-- 内容开始 -->
<view class='content content1'>
<text>我是内容1</text>
</view>
<view class='content content2'>
<text>我是内容2</text>
</view>
<view class='content content3'>
<text>我是内容3</text>
</view>
<view class='content content4'>
<text>我是内容4</text>
</view>
<!-- 内容结束 -->
</scroll-view>

wxss:

.clearfix:after {
content: ".";
display: block;
height:;
clear: both;
visibility: hidden;
}
.positionFixed{
position: fixed;
left:;
top:;
} page{
width: 100%;
height:100%;
}
.layout{
width: 100%;
height: 100%;
background: #eee;
}
.banner{
width: 100%;
height: 200px;
line-height: 200px;
background: #FFB11A ; }
.banner text{
text-align: center;
display: block;
}
.nav{
width: 100%;
height: 45px;
line-height: 45px;
background: #fff;
}
.nav_row{
float: left;
width: 25%;
font-family: PingFangSC-Light;
font-size: 16px;
color: #333333;
}
.nav_row text{
text-align: center;
display: block;
}
.content {
width: 100%;
height: 200px;
font-family: PingFangSC-Light;
font-size: 16px;
color: #333333;
padding: 15px;
}
.content1{
background: #F5BBA4;
}
.content2{
background: #E9ED9A;
}
.content3{
background: #9DE59C;
}
.content4{
background: #98A5E2;
}

js:

Page({
data: {
scrollTop:'', //滑动的距离
navFixed:false, //导航是否固定
},
//页面滑动
layoutScroll: function (e) {
this.data.scrollTop = this.data.scrollTop * 1 + e.detail.deltaY * 1;
console.log(this.data.scrollTop)
console.log(this.data.navFixed)
if (this.data.scrollTop <= -200){
this.setData({
navFixed:true
})
}else{
this.setData({
navFixed: false
})
}
}
})

这个代码能基本实现需求,但是存在很大的弊端:

1.导航固定后,页面卡顿一下

2.导航效果延迟较长,用户体验很差

总体来说这种方案并不可取,所以进行第二次迭代

思路2:

未完待续。。。

注意:

1.整个滑动的页面应该写在scroll-view中;

2.scroll-view一定

微信小程序--导航跟随的更多相关文章

  1. 微信小程序-导航 & 路由

    微信小程序-导航 & 路由 页面跳转 页面路由 页面栈, 框架以栈的形式维护了当前的所有页面. https://developers.weixin.qq.com/miniprogram/dev ...

  2. 自定义微信小程序导航(兼容各种手机)

    详细代码请见github,请点击地址,其中有原生小程序的实现,也有wepy版本的实现 了解小程序默认导航 如上图所示,微信导航分为两部分,第一个部分为statusBarHeight,刘海屏手机(iPh ...

  3. 微信小程序——导航栏组件

    组件内属性详解   属性 类型 默认值 必填 说明 nav-postion String relative 否 导航栏(包含导航栏以及状态栏)的position,可取值relative.fixed.a ...

  4. 微信小程序导航:官方工具+精品教程+DEMO集合(1月7更新)

    1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=14764346784612:简易教程:https://mp.weixin.qq.com/debug ...

  5. 微信小程序------导航栏样式、tabBar导航栏

    一:导航栏样式设置 小程序的导航栏样式在app.json中定义. 这里设置导航,背景黑色,文字白色,文字内容测试小程序 app.json内容: { "pages":[ " ...

  6. 微信小程序 导航(a 连接)自定义组件

    导航:navigator 组件 组件上的属性: target:跳到其他小程序( 默认是当前小程序 ),当属性值为 miniProgram 时,跳到别的小程序(如果要跳到别的小程序,需要填写 appid ...

  7. 微信小程序导航栏,下面内容滑动,上册导航栏跟着滑动,内容随着导航栏滑动

    16.类似微信导航栏滑动.png 今日头条导航栏,下面滑动上面跟着滑动 index.wxml <swiper class="content" style="heig ...

  8. 微信小程序---导航

    1.wx.navigateTo(OBJECT):保留当前页面,跳转到应用内的某个页面,使用wx.navigateBack可以返回到原页面. wx.navigateTo({ url: 'test?id= ...

  9. 微信小程序导航设置

    "tabBar": { "backgroundColor": "#ffffff", "color": "#00 ...

随机推荐

  1. IDEA引入jar但无法导入class

    如图,jar已经导入,但是无法import class 选择setting->maven->Ignored Files,将被忽略的model取消选中就可以解决了

  2. 云-腾讯云-云点播:云点播(VOD)

    ylbtech-云-腾讯云-云点播:云点播(VOD) 提供端到端的一站式VpaaS音视频点播解决方案 1.返回顶部 1. 云点播(Video on Demand,VOD)基于腾讯多年技术积累与基础设施 ...

  3. pinmap 和 pin allocation

    串口管脚分配

  4. java读写属性配置文件

    package readproperties; import java.io.FileInputStream; import java.io.IOException; import java.io.I ...

  5. python 对excel操作

    在python中,对excel表格读,写,追加数据,用以下三个模块:1.wlrd 读取excel表中的数据2.xlwt 创建一个全新的excel文件,然后对这个文件进行写入内容以及保存.3.xluti ...

  6. Java中9大内置基本数据类型Class实例和数组的Class实例

    1.Java中9大内置几本数据类型: 对于对象来说,可以直接使用对象.getClass()或者Class.forName(className);.类名.class都可以获取Class实例. 但是我们的 ...

  7. 分布式日志收集之Logstash 笔记(一)

    (一)logstash是什么? logstash是一种分布式日志收集框架,开发语言是JRuby,当然是为了与Java平台对接,不过与Ruby语法兼容良好,非常简洁强大,经常与ElasticSearch ...

  8. 保持SSH连接的linux服务器不断线

    目录 1. secureCRT和putty连接设置 2. 命令行设置 3. 服务器配置修改 4. 参考 使用ssh连接远程服务器的时候,如果长时间没有操作,远程连接就有可能中断.正在执行的程序和vim ...

  9. JavaScript中定义函数的几种方式

    函数的组成:函数名 + 函数体 1.使用function关键字定义函数 -- 具有优先级,优先将function关键字定义的函数优先执行 function  functionName(arg0, ar ...

  10. 关于Loadrunner非常好的英文网站

    关于Loadrunner非常好的英文网站 今天无意间在一个测试同行的BLOG中发现了这个网站的链接: http://www.wilsonmar.com/1loadrun.htm 非常棒的一个网站,里面 ...