vue实现锚点跳转:scrollIntoView()

文章介绍了在Vue中如何使用scrollIntoView()方法实现元素的锚点跳转,包括如何滚动到特定元素如form或div的底部。通过示例代码展示了如何获取元素并调用方法使其回到可视区域。还详细解释了scrollIntoView的参数用法,如true和false分别对应元素顶部和底部对齐,以及通过配置对象实现平滑滚动的效果。需要注意的是,IE浏览器对某些高级参数的支持情况。

vue实现锚点跳转:scrollIntoView()

说明:
滚动到某个特定元素 :scrollIntoView();例如form表单或者div滚动到底部,

document.getElementsByClassName(‘’)或者document.getElementsById(‘’)

获取到元素后即可实现回到可视化区域(也可理解为回到顶部)。

使用案例:

<div> v-for="(value,index) in data" class="roll">{{...}}</div>  

js部分

methods:{
  scrollToPosition(index){
     document.getElementsByClassName('roll')[index].scrollIntoView()
}

这样就利用scrollIntoView()简单实现了一个锚点跳转,下边讲解scrollIntoView中的一些属性:

scrollIntoView(true)相等于scrollIntoView();元素的顶端将和其所在滚动区的可视区域的顶端对齐
为true时相应的 scrollIntoViewOptions: {block: “start”, inline:
“nearest”}。这是这个参数的默认值。

scrollIntoView(false)元素的底端将和其所在滚动区的可视区域的底端对齐
为false时相应的scrollIntoViewOptions: {block: “end”, inline: “nearest”}。

同时他的参数也可以配置成一个object对象

  scrollIntoView({
  behavior:auto //定义动画过渡效果"auto""smooth" 之一。默认为 "auto"。
  block:start//定义垂直方向的对齐, "start", "center", "end", 或 "nearest"之一。默认为 "start"。
  inline:nearest//"start", "center", "end", 或 "nearest"之一。默认为 "nearest"})
  • 其中smooth是平滑滚动 start和end是目标滚动到的位置
    注意:兼容性的问题多数主流浏览器已经支持其基本功能,也就是说,使用true,false两个参数,来实现木讷的定位(没有滚动动画)是没有任何问题的,但是传入object参数时,IE各种版本会直接忽略,全部看成true参数属性,如果想看到滚动动画,使用火狐和chrome。
在uni-app和Vue 3中实现跳转,可参考以下思路。 ### 利用`uni.createSelectorQuery`和`uni.pageScrollTo` 通过`uni.createSelectorQuery`获取DOM节距离顶部的距离,再使用`uni.pageScrollTo`滑动页面。 示例代码如下: ```vue <template> <!-- NAV部分 --> <view v-for="(item, index) in arr" :key="index" @click="point(index)"> <text>{{ item.name }}</text> </view> <!-- 内容部分 --> <view class="newShopCon"> <view :class="'tab_item' + index" v-for="(item, index) in arr" :key="index"> <view>这里是内容</view> </view> </view> </template> <script setup> import { ref } from 'vue'; const arr = ref([ { name: '第一个' }, { name: '第二个' }, // 可添加更多 ]); const point = (index) => { // 要跳转的目标位置的节 uni.createSelectorQuery().select('.tab_item' + index).boundingClientRect((data) => { // 最外层盒子的节 uni.createSelectorQuery().select('.newShopCon').boundingClientRect((res) => { uni.pageScrollTo({ duration: 100, // 过渡时间 scrollTop: data.top - res.top, // 到达距离顶部的top值 }); }).exec(); }).exec(); }; // 如果想要获得实时滑动的位置 根据滑动位置改变NAV当前选中高亮 const onPageScroll = (e) => { // e.scrollTop 是当前页面滑动距离 }; </script> ``` 上述代码参考了引用[1]的思路,在Vue 3的`<script setup>`语法糖下进行了改写。 ### 利用`scrollIntoView` 在Vue 3中也可使用`scrollIntoView`方法实现平滑滚动到目标元素。 示例代码如下: ```vue <template> <div> <ul> <li v-for="(item, index) in arr" :key="index" @click="liChange(index)">{{ item }}</li> </ul> <div v-for="(item, index) in arr" :key="index" :id="'li' + index"> <p>{{ item }}</p> </div> </div> </template> <script setup> import { ref } from 'vue'; const arr = ref([ '跳到第一个盒子', '跳到第二个盒子', '跳到第三个盒子', '跳到第四个盒子', '跳到第五个盒子', ]); const liChange = (i) => { document.querySelector('#li' + i).scrollIntoView({ behavior: 'smooth', // 平滑移动到目标元素 block: 'start', inline: 'nearest', }); }; </script> ``` 上述代码参考了引用[2]的思路,在Vue 3的`<script setup>`语法糖下进行了改写。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值