Home >Web Front-end >uni-app >How to implement anchor point jump in uniapp

How to implement anchor point jump in uniapp

coldplay.xixi
coldplay.xixiOriginal
2020-12-09 16:34:076757browse

How uniapp implements anchor point jump: Use uniapp's [uni.createSelectorQuery()] method in combination with the [uni.pageScrollTo(OBJECT)] method.

How to implement anchor point jump in uniapp

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(&#39;.comment-content&#39;).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(&#39;.comment-content&#39;).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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use uniappNext article:How to use uniapp