Home > Article > Web Front-end > How to implement anchor point jump in uniapp
How uniapp implements anchor point jump: Use uniapp's [uni.createSelectorQuery()] method in combination with the [uni.pageScrollTo(OBJECT)] method.
The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, Dell G3 computer.
Recommended (free): uni-app development tutorial
uniapp implements anchor point jump Method:
Combine uniapp’s uni.createSelectorQuery()
method with the uni.pageScrollTo(OBJECT) method
Core Code
//从当前位置到达目标位置 uni.createSelectorQuery().select('.comment-content').boundingClientRect(data=>{//目标位置的节点:类或者id uni.createSelectorQuery().select(".arc-content").boundingClientRect(res=>{//最外层盒子的节点:类或者id uni.pageScrollTo({ duration: 100,//过渡时间 scrollTop:data.top - res.top ,//到达距离顶部的top值 }) }).exec() }).exec();
Code example
<template> <view class="arc-content" id="arc-content"> <view class="info-content">文章区域。。。</view> <view class="comment-content" id="comment-content">评论区域。。。</view> </view> </template> togglePosition(){ if(this.positionSelect){ this.positionSelect=false //从评论区域回到顶部 uni.createSelectorQuery().select('.comment-content').boundingClientRect(data=>{//目标位置的节点:类或者id uni.createSelectorQuery().select(".arc-content").boundingClientRect(res=>{//最外层盒子的节点:类或者id uni.pageScrollTo({ duration: 100,//过渡时间 scrollTop:res.top - data.top ,//返回顶部的top值 }) }).exec() }).exec(); }else{ this.positionSelect=true //从当前位置到达评论区域 uni.createSelectorQuery().select('.comment-content').boundingClientRect(data=>{//目标位置的节点:类或者id uni.createSelectorQuery().select(".arc-content").boundingClientRect(res=>{//最外层盒子的节点:类或者id uni.pageScrollTo({ duration: 100,//过渡时间 scrollTop:data.top - res.top ,//到达距离顶部的top值 }) }).exec() }).exec(); } },
Related free learning recommendations: Programming video
The above is the detailed content of How to implement anchor point jump in uniapp. For more information, please follow other related articles on the PHP Chinese website!