
TL;DR: 左右联动的本质是维护两个状态:左侧当前分类 `tabCur`,右侧滚动目标 `rightCur`。点击左侧时设置右侧 `scroll-into-view`;滑动右侧时根据滚动高度判断当前分类,再同步左侧高亮和滚动位置。 适合正
**TL;DR:**左右联动的本质是维护两个状态:左侧当前分类 tabCur,右侧滚动目标 rightCur。点击左侧时设置右侧 scroll-into-view;滑动右侧时根据滚动高度判断当前分类,再同步左侧高亮和滚动位置。
适合正在做点单页、商品分类页、文档目录页的小程序开发者。
如果页面只是简单 tab 切换,不需要这套联动。如果右侧内容很长,分类数量多,这个方案能解决“左侧目录和右侧内容不同步”的问题。
源笔记给出的交互目标有两个:
实现这两个目标需要三个数据:
data: {
list: category,
scrollTops: 0,
tabCur: 0,
rightCur: 0
}
tabCur:左侧当前激活项。rightCur:右侧 scroll-into-view 的目标索引。scrollTops:左侧 scroll-view 需要滚动到的位置。源笔记强调了一个关键点:右侧循环项必须加上 id="scroll-{{index}}"。没有这个 id,scroll-into-view 找不到目标元素,左侧点击无法带动右侧滚动。
<view class="link">
<scroll-view class="left" scroll-y scroll-with-animation scroll-top="{{scrollTops}}">
<view
class="item {{tabCur===index?'active':''}}"
wx:for="{{list}}"
wx:key="index"
data-index="{{index}}"
bindtap="tabNav"
>
{{item.name}}
</view>
</scroll-view>
<scroll-view
class="right"
scroll-y
scroll-with-animation
scroll-into-view="scroll-{{rightCur}}"
bindscroll="scrollLink"
>
<view wx:for="{{list}}" wx:key="index" class="right-cont" id="scroll-{{index}}">
<view class="title">{{item.name}}</view>
<view class="content">
<view
class="list-item"
wx:for="{{item.list}}"
wx:key="index"
wx:for-index="indexs"
wx:for-item="items"
>
{{items}}
</view>
</view>
</view>
</scroll-view>
</view>
右侧 id 和 rightCur 的拼接规则必须一致:scroll-0、scroll-1、scroll-2。
源笔记的样式结构很直接:
.link {
width: 100%;
height: 100%;
display: flex;
}
.left {
width: 220rpx;
height: 100vh;
background-color: rgb(229, 243, 243);
text-align: center;
color: #333;
}
.left .item {
line-height: 50px;
font-size: 34rpx;
}
.active {
background-color: tomato;
color: #fff;
}
.right {
height: 100vh;
background-color: #f8f8f8;
padding: 0 30rpx;
}
这里的重点是左右两列都用 scroll-view,并保证容器有高度。没有固定高度时,滚动行为容易不符合预期。
点击左侧分类时,逻辑很简单:
tabNav(e) {
let index = e.currentTarget.dataset.index;
this.setData({
tabCur: index,
rightCur: index,
scrollTops: (index - 4) * 50
})
}
rightCur 改变后,右侧 scroll-into-view="scroll-{{rightCur}}" 会把对应内容滚到视图内。
scrollTops: (index - 4) * 50 是左侧菜单的滚动位置修正。源笔记里的左侧项行高是 50px,所以这里按索引计算滚动距离。
右侧滚动时,需要知道每个右侧内容块在整体滚动区域中的 top 和 bottom。源笔记使用 wx.createSelectorQuery() 获取每个内容块高度:
scrollLink(e) {
let list = this.data.list
let itemHeight = 0;
for (let i = 0; i < list.length; i++) {
let els = wx.createSelectorQuery().select("#scroll-" + i);
els.fields({
size: true
}, function (res) {
list[i].top = itemHeight;
itemHeight += res.height;
list[i].bottom = itemHeight
}).exec()
}
this.setData({
list
})
let scrollTop = e.detail.scrollTop;
for (let i = 0; i < list.length; i++) {
if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
this.setData({
tabCur: i,
scrollTops: (i - 4) * 50
})
return false
}
}
}
判断条件是:当前滚动距离落在哪个内容块的 [top, bottom] 区间内,哪个分类就高亮。
右侧内容块 id 必须存在,并且和 scroll-into-view 的值完全对应。
左侧自动滚动依赖固定行高。源笔记使用 (index - 4) * 50,如果你改了 .left .item 的高度,也要同步修改这里。
右侧内容块高度来自实际渲染结果,不要硬编码。不同分类下内容条数不一样,硬编码会导致滑动反推错误。
源代码在每次右侧滚动时都重新查询所有右侧内容块高度。分类数量少时问题不大,分类很多或滚动频繁时,会有性能压力。
更稳的做法是:页面渲染完成后计算一次 top/bottom,内容变化时再重算。这样 scrollLink 里只做区间判断。
另外,示例数据来自 category.js 的静态数组。如果数据来自接口,要等数据渲染完成后再计算高度。
下一步可以把高度计算抽成 calcRightItemBounds(),在 onReady 或接口数据设置完成后调用。这样滚动事件只负责读 e.detail.scrollTop 和更新 tabCur。
今日AI领域节奏平缓,DeepSeek V4.1 Flash、MiniMax M3.1 Flash和Mercury 3三款新模型亮相,但短期难以大规模落地。Vercel AI SDK与LangChain.js持续迭代,新增推理参数并完善网关路由。纽约市长呼吁加强AI监管。
OpenAI为GPT-6.2 Astra与Sol强化推理防提取,红队提取成功率从54%降至0.2%,并每周处置数千至数万账号打击协同蒸馏。这提示开发者API行为或更保守,企业需重估蒸馏与微调合规风险,模型能力开放与知识产权保护张力上升。
Codex 好不好用,关键不在提示词,而在工作方式。本文整理 10 条实战技巧:侧边聊与主会话分工、多 Agent 并行、按任务选模型、讨论落成 Spec、按根因批量修 Bug、七成时间做规划、续接旧会话、用 Skill 固化 SOP、多 Agent 交叉审查,以及让 AI 说大白话。
新内容发布后发送确认邮件,不会把未验证或已退订地址加入发送列表。
欢迎理性讨论与补充勘误
无需登录即可评论,展示名称会使用脱敏 IP。
暂无评论