1:你的 onPageScroll 事件是不是失灵?闲话不多说,直接上代码!!!

<!--pages/homePage/testing/testing.wxml-->
<view class="list">
<view class="{{ toView=='list0'?'colorBlue':'colorGray' }}">list0</view>
<view class="{{ toView=='list29'?'colorBlue':'colorGray' }}">list29</view>
<view class="{{ toView=='list59'?'colorBlue':'colorGray' }}">list59</view>
<view class="{{ toView=='list89'?'colorBlue':'colorGray' }}">list89</view>
</view>
<!-- 绑定事件 开始滑动事件 滑动过程事件 滑动结束事件 -->
<scroll-view
scroll-y = "true"
scroll-with-animation = "true"
class = "{{ heightType=='heightHandred' ? 'heightHandred' : 'heightAuto' }}"
>
<view
wx:for="{{list}}"
id="{{item}}"
wx:key="{{index}}"
class="knowledgeNodes">
{{item}}
</view>
</scroll-view>

2:js片段(监听页面上滑和下滑事件,当左侧对应的值到达顶部,右侧对应的值变蓝色)。

// pages/homePage/testing/testing.js

Page({
data: {
tabScrollTop: 0,
tabFixed: false,
list: [ "list0", "list1", "list2", "list3", "list4", "list5", "list6", "list7", "list8", "list9",
"list10", "list11", "list12", "list13", "list14", "list15", "list16", "list17", "list18", "list19",
"list20", "list21", "list22", "list23", "list24", "list25", "list26", "list27", "list28", "list29",
"list30", "list31", "list32", "list33", "list34", "list35", "list36", "list37", "list38", "list39",
"list40", "list41", "list42", "list43", "list44", "list45", "list46", "list47", "list48", "list49",
"list50", "list51", "list52", "list53", "list54", "list55", "list56", "list57", "list58", "list59",
"list60", "list61", "list62", "list63", "list64", "list65", "list66", "list67", "list68", "list69",
"list70", "list71", "list72", "list73", "list74", "list75", "list76", "list77", "list78", "list79",
"list80", "list81", "list82", "list83", "list84", "list85", "list86", "list87", "list88", "list89",
"list90", "list91", "list92", "list93", "list94", "list95", "list96", "list97", "list98", "list99",
],
toView: '',
chapterTopArr: [],
heightType: 'heightAuto'
}, onLoad: function(){
this.initData();
}, // 获取知识点节点 selector
getAllKNodes: function(){
const _this = this;
// 循环所有 knowledgeNodes 项
wx.createSelectorQuery().selectAll('.knowledgeNodes').boundingClientRect(rects=>{
var chapterTopArr = [];
rects.forEach(function(rect){
switch (rect.id){
case 'list9':
chapterTopArr.push(rect.top);
break;
case 'list19':
chapterTopArr.push(rect.top);
break;
case 'list29':
chapterTopArr.push(rect.top);
break;
case 'list39':
chapterTopArr.push(rect.top);
break;
case 'list49':
chapterTopArr.push(rect.top);
break;
case 'list59':
chapterTopArr.push(rect.top);
break;
case 'list69':
chapterTopArr.push(rect.top);
break;
case 'list79':
chapterTopArr.push(rect.top);
break;
case 'list89':
chapterTopArr.push(rect.top);
break;
case 'list99':
chapterTopArr.push(rect.top);
break;
default:
break;
}
})
_this.setData({
chapterTopArr : chapterTopArr
})
}).exec()
}, // 初始化数据 (获取当前所在章节,对应的知识点)
initData: function(){
this.setData({
toView: 'list9'
})
this.getAllKNodes();
}, // 监听页面高度(上滑或者下滑)
onPageScroll: function(res) {
const _this = this;
const scrollTop = res.scrollTop;
const chapterTopArr = _this.data.chapterTopArr;
if(chapterTopArr.length !== 0){
if( chapterTopArr[0] <= scrollTop && scrollTop < chapterTopArr[0]){
_this.setData({
toView: 'list9'
})
}else if( chapterTopArr[1] <= scrollTop && scrollTop < chapterTopArr[2] ){
_this.setData({
toView: 'list19',
})
}else if( chapterTopArr[2] <= scrollTop && scrollTop < chapterTopArr[3] ){
_this.setData({
toView: 'list29'
})
}else if( chapterTopArr[3] <= scrollTop && scrollTop < chapterTopArr[4] ){
_this.setData({
toView: 'list39'
})
}else if( chapterTopArr[4] <= scrollTop && scrollTop < chapterTopArr[5] ){
_this.setData({
toView: 'list49'
})
}else if( chapterTopArr[5] <= scrollTop && scrollTop < chapterTopArr[6] ){
_this.setData({
toView: 'list59'
})
}else if( chapterTopArr[6] <= scrollTop && scrollTop < chapterTopArr[7] ){
_this.setData({
toView: 'list69'
})
}else if( chapterTopArr[7] <= scrollTop && scrollTop < chapterTopArr[8] ){
_this.setData({
toView: 'list79'
})
}else if( chapterTopArr[8] <= scrollTop && scrollTop < chapterTopArr[9] ){
_this.setData({
toView: 'list89'
})
}else{ }
}else{
return false;
}
}
})  

3:看了前两部没什么了不起的,但是坑来了,对就是 css 样式的坑( 不信的话,将以上 js 中 data heightType值改成 "heightHandred" 试试,你就会恍然大迷瞪!!! ):

/* pages/homePage/testing/testing.wxss */
page{
height: 100%;
}
/* 左侧 list 列表 */
.heightHandred{
position: relative;
height: 100%;
}
.heightAuto{
position: relative;
height: auto;
}
view.knowledgeNodes{
height: 200rpx;
line-height: 200rpx;
} /* 右侧 list 表头跳转页 */
.list{ position: fixed; z-index:; top:30rpx; right: 10rpx; } .colorBlue{
color: deepskyblue;
}
.colorGray{
color: darkslategray;
}

4:css 预留 样式 “heightAuto” => 为了下一讲,绑定滚动条事件 bindscroll 事件 和 锚点跳转 ~~~ 感觉不错就点个赞吧

02_小程序——onPageScroll 你入坑了吗?的更多相关文章

  1. 小程序onLaunch事件的坑

    记一个小程序踩过的坑 小程序项目中app.js里面定义了globalData,即全局变量,里面定义了一个token字段 需求是这样的,每次进入小程序的时候需要检验该token有没有,没有就请求后台获取 ...

  2. 微信小程序支付遇到的坑

    1,微信公众号支付和微信小程序支付有差异 微信公众号:可以直接跳转走h5的微信支付 微信小程序:在测试环境.沙箱环境使用微信公众号的跳转支付没有问题,在线上存在支付异常 最后商讨的解决方法 openi ...

  3. 小程序textarea完美填坑

    相信做微信小程序的码友们都被textarea这个原生组件坑过,什么placeholder位置错乱,穿透弹窗或遮罩层,ios上输入法弹起后换行输入内容遮挡,删除输入内容时内容被遮挡等等... 反正综上所 ...

  4. 如果解决小程序1024kb渲染之坑

    问题: 在小程序开发中如果有那么个场景和操作步骤,获取商品下拉列表商品列表data为goodsList 当从后台获取数据response.data.list,通常我们会setData({goodsLi ...

  5. 微信小程序开发常见之坑

    https://www.cnblogs.com/shunxing/articles/6971648.html input里的value会在浮层上面的,要解决这一问题还是很简单的,在小程序中input有 ...

  6. 小程序:web-view采坑指南

    最近负责开发的[广州医保查询]小程序已经发布上线,其中使用web-view组件完成的[在线绑定社保卡]核心流程,遇到了一些坑,现总结如下: 首先,让我们一起看看什么是web-view ? 小程序api ...

  7. 微信小程序—setTimeOut定时器的坑

    原文地址: http://fanjiajia.cn/2018/06/27/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E2%80%94setTimeOu ...

  8. 小程序红包开发跳坑记 微信小程序红包接口开发过程中遇到的问题 微信小程序红包开发

    现在做小程序的越来越多,商家推广也是一个瓶颈,谁不发点红包,都很难找到人来用你的微信小程序了.于是不管你开发什么小程序功能,你或多或少都要用到小程序来发红包吧.  我们自己之前做公众号发红包,做了两三 ...

  9. 小程序框架MpVue踩坑日记(一)

    小程序也做了几个小功能模块了,总觉得需要总结一下,踩坑什么的还是得记录一下啊. 好吧,其实是为了方便回顾 首先,说到小程序框架,大家都知道wepy,不过,我是没用过 美团开发团队到mpvue到是个实在 ...

随机推荐

  1. props & children

    一. choosing the type at runtime import React from 'react'; import { PhotoStory, VideoStory } from '. ...

  2. Laravel 5.6 安装 guzzlehttp

    环境:Laravel 5.6 安装  composer require guzzlehttp/guzzle 在vendor文件夹下,vendor\guzzlehttp\guzzle 引入 use Gu ...

  3. jq实现简单手风琴效果

    文章地址:https://www.cnblogs.com/sandraryan/ 利用slideUp slideDown动画 <!DOCTYPE html> <html lang=& ...

  4. ip地址库 与浏览器的关系

    https://zhidao.baidu.com/question/325152705.html 只要手机连接数据上网就会产生ip,只要进入了淘宝,就能查出用户访问记录的. 手机是运营商动态分配的.它 ...

  5. PHP调用纯真IP数据库返回具体地址

    function convertip($ip) { $ip1num = 0; $ip2num = 0; $ipAddr1 =""; $ipAddr2 =""; ...

  6. meta标签、常用的文字类标签及其区别

    常用的文字类基本标签 段落:p标题文字 :h1~h6超链接:a,必须属性href,后跟跳转地址图片:img,必须属性src,后跟图片地址字体斜体:em.i 文字加粗:b.strong文字下划线:u文字 ...

  7. Linux 查看kafka版本

    find /opt -name \*kafka_\* | head -1 | grep -o '\kafka[^\n]*'

  8. linux 如何编译安装软件

  9. Python--day31--黏包(不熟...)

  10. Intellij Idea更换主题

    <h1 class="title">Intellij Idea更换主题</h1> <!-- 作者区域 --> <div class=&qu ...