uniapp如何实现锚点跳转

发布时间:2022-05-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了uniapp如何实现锚点跳转脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

uniapP实点跳转的方法:将uniapp的【uni.createSelectorQuery()】方法与【uni.pageScrollTo(OBJECT)】方法结合使用即可。

uniapp如何实现锚点跳转

本教程操作环境: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如何实现锚点跳转的详细内容,更多请关注脚本宝典其它相关文章!

脚本宝典总结

以上是脚本宝典为你收集整理的uniapp如何实现锚点跳转全部内容,希望文章能够帮你解决uniapp如何实现锚点跳转所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。