您好,欢迎访问一九零五行业门户网

uniapp如何实现锚点跳转

uniapp实现锚点跳转的方法:将uniapp的【uni.createselectorquery()】方法与【uni.pagescrollto(object)】方法结合使用即可。
本教程操作环境:windows7系统、uni-app2.5.1版本,dell g3电脑。
推荐(免费):uni-app开发教程
uniapp实现锚点跳转的方法:
将uniapp的uni.createselectorquery()方法与uni.pagescrollto(object)方法结合使用
核心代码
//从当前位置到达目标位置 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();
代码示例
<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(); }},
相关免费学习推荐:编程视频
以上就是uniapp如何实现锚点跳转的详细内容。
其它类似信息

推荐信息