当前位置: 首页 > news >正文

小程序原生实现左右锚点联动

效果
在这里插入图片描述

wxml

<view class='box'><scroll-view scroll-y scroll-with-animation style="width:25%"><view class='nav'><view wx:for="{{navList}}" wx:key='index' class="title {{index == active ?'select':''}}"data-index='{{index}}' bindtap='activeNav'>{{item}}</view></view></scroll-view><scroll-view scroll-y style="width:75%" scroll-with-animation scroll-into-view="{{selectId}}"bindscroll="watchScroll"><view class='content'><view id='{{"item"+index}}' class='subtitle' wx:for="{{navList}}" wx:key='index'>{{item}}</view></view></scroll-view>
</view>

css

.box {display: flex;
}
.nav {height: 100%;width: 100%;background: #F5F5F5;box-sizing: border-box;flex-wrap: wrap;
}.title {box-sizing: border-box;width: 100%;padding: 32rpx;font-size: 28rpx;
}.select {background: #fff;border-left: 5rpx solid #eec718;box-sizing: border-box;
}.content {padding: 0 30rpx;box-sizing: border-box;width: 100%;height: 100vh;
}.subtitle {width: 100%;height: 650rpx;border-bottom: 10rpx #f5f5f5 solid;
}

js

Page({data: {heightArr: [],distance: 0,active: 0,selectId: "item0",navList: ['全部甜品', '今日甜品系列', '毛巾卷系列', '切块系列', '限时限量系列', '提拉米苏系列']},onLoad: function (options) {this.selectHeight();},// 选择左侧标签锚点定位activeNav(e) {var index = e.currentTarget.dataset.indexthis.setData({active: index,selectId: "item" + index})},//计算右侧每个锚点的高度selectHeight() {var list = []var height = 0;const query = wx.createSelectorQuery();query.selectAll('.subtitle').boundingClientRect()query.exec((res) => {res[0].forEach((item) => {height += item.height;list.push(height)})this.data.heightArr = list})},//监听scroll-view的滚动事件watchScroll(e) {let scrollTop = e.detail.scrollTop; //获取距离顶部的距离let active = this.data.active;if (scrollTop >= this.data.distance) {if (active + 1 < this.data.heightArr.length && scrollTop >= this.data.heightArr[active]) {this.setData({active: active + 1})}} else {if (active - 1 >= 0 && scrollTop < this.data.heightArr[active - 1]) {this.setData({active: active - 1})}}this.data.distance = scrollTop;}
})
http://www.lryc.cn/news/108034.html

相关文章:

  • STM32 低功耗-睡眠模式
  • IDEA用Gradle构建项目时,lombok插件无效的解决办法
  • 基于方向编码的模板匹配算法matlab仿真
  • shell centos 7 一键部署 KVM软件脚本
  • 64 # 实现一个 http-server
  • HCIP作业3
  • 【测试学习三】软件测试的生命周期 BUG的相关知识
  • git rebase 的坑儿
  • SSM(Vue3+ElementPlus+Axios+SSM前后端分离)【四】
  • iPhone 8 Plus透明屏应用范围详解
  • 【前端面试手撕题】instanceof、Array.map、Array.filter、Array.reduce、_objectCreate
  • 8.物联网操作系统之事件标志组
  • [腾讯云Cloud Studio实战训练营]无门槛使用GPT+Cloud Studio辅助编程完成Excel自动工资结算
  • 局域网ssh登录windows自带Linux系统(WSL)踩坑记录
  • 2023-02-03——2023-08-03,半年以来与客服交流的记录【CSND 文章撰写 网站使用求解】客服咨询交流记录(长期更新ing)
  • DCL 操作
  • C++11移动构造函数详解
  • 【力扣】19. 删除链表的倒数第 N 个结点 <链表指针、快慢指针>
  • Vue3和typeScript路由传参
  • SQLserver数据库巡检脚本
  • Go Ethereum源码学习笔记 001 Geth Start
  • idea如何加快创建Maven项目的速度
  • 软件外包开发的GO开发框架
  • oracle会话打满
  • VSCode自定义闪烁光标
  • 复亚智能打造全新云平台:让无人机任务管理更智能、更简单
  • 编程导航第六关——白银挑战
  • 743. 网络延迟时间
  • Kubernetes高可用集群二进制部署(四)部署kubectl和kube-controller-manager、kube-scheduler
  • 在CSDN学Golang场景化解决方案(微服务架构设计)