網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
微信小程序?qū)崿F(xiàn)滾動(dòng)視圖點(diǎn)擊錨點(diǎn)跳轉(zhuǎn)、點(diǎn)擊左側(cè)分欄時(shí)右側(cè)對(duì)應(yīng)內(nèi)容置頂、左右分欄聯(lián)動(dòng)、setTimeout、findIndex、setData、parseInt、Math、random、forEach
作者:web半晨 更新時(shí)間: 2022-06-06 編程語(yǔ)言1、關(guān)鍵代碼
// 滾動(dòng)時(shí)觸發(fā)
bindscroll({detail: {scrollTop}}) {
let that = this,
titleContenData = that.data.titleContenData;
// 防抖
// timer定義在全局
// 如果沒(méi)有防抖會(huì)觸發(fā)許多次
// 對(duì)性能不友好
if (timer !== undefined) clearTimeout(timer);
timer = setTimeout(function () {
// 當(dāng)右側(cè)滾動(dòng)到頂部時(shí)強(qiáng)制賦值為0
// 因?yàn)樵跐L動(dòng)時(shí)一般獲取到的數(shù)據(jù)是0-10的范圍
// 小概率會(huì)獲取到0
// 因?yàn)樵却鎯?chǔ)viewTop屬性的第一個(gè)值就是0
scrollTop = scrollTop < 10 ? 0 : scrollTop;
let selectIndex = titleContenData.findIndex((item) => item.viewTop >= scrollTop);
that.setData({
// 設(shè)置高亮
selectIndex,
// 此屬性聯(lián)動(dòng)左側(cè)滾動(dòng)條
// 當(dāng)右側(cè)滾動(dòng)時(shí)
// 左側(cè)也會(huì)相應(yīng)的滾動(dòng)
// 只是滾動(dòng)的距離不一樣
scrollTop: 5 * that.data.selectIndex
});
}, 100);
}
微信小程序有兩個(gè)版本,此處展示柔和版的代碼,還有一個(gè)版本是生硬版。兩個(gè)版本的區(qū)別在于右側(cè)滑動(dòng)(滾動(dòng))時(shí),頁(yè)面動(dòng)畫(huà)和定位的位置不同,建議使用柔和版。兩個(gè)版本存在的共同問(wèn)題是,當(dāng)內(nèi)容倒數(shù)部分如果數(shù)據(jù)比較少,首次點(diǎn)擊左側(cè)倒數(shù)部分的標(biāo)簽會(huì)發(fā)生回跳到對(duì)應(yīng)位置,此問(wèn)題暫未解決。
2、完整代碼
1、gitee(碼云) - develop分支 - leftAndRightColumnsSoft 文件夾-柔和版
2、gitee(碼云) - develop分支 - leftAndRightColumnsStiff 文件夾-生硬版
原文鏈接:https://blog.csdn.net/weixin_51157081/article/details/124053442
相關(guān)推薦
- 2023-01-17 React?Hooks核心原理深入分析講解_React
- 2021-12-19 Linux下wget命令詳細(xì)介紹_Linux
- 2022-05-06 SQL查詢(xún)服務(wù)器下所有數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)的全部表
- 2023-03-30 官網(wǎng)項(xiàng)目Jetpack?Startup庫(kù)學(xué)習(xí)_Android
- 2022-06-01 AndriodStudio使用listview實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理_Android
- 2023-07-04 注冊(cè)npm上傳包報(bào)This operation requires a one-time passwo
- 2023-01-19 python圖形界面教程Tkinter詳解_python
- 2022-09-01 C++?OpenCV實(shí)戰(zhàn)之形狀識(shí)別_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支