本文最后更新于 2024-08-06T09:02:22+08:00
1、给子元素设置唯一ref或id
1
| <div class="title-box" v-for="item in arr" :key="item.id" :id="item.id"></div>
|
2、给父元素设置ref或者id
1
| <div ref="ItemBoxRef" id="itembox" ></div>
|
3、给元素绑定点击事件
1
| <div @click="scrollToSection(id)"><div>
|
4、实现方法
1 2 3 4 5 6 7 8
| const scrollToSection = (id) => {
const element = document.getElementById(id); const scrollableParent = document.getElementById("itembox"); if (element && scrollableParent) { scrollableParent.scrollTop = element.offsetTop - 100; } };
|
vue中使用锚点定位
https://mengluo.com/2024/08/02/vue中使用锚点定位/