vue锚点跳转

// 获取dom元素
const ele = this.$refs['refName'];
// 获取元素距视窗的高度
const top1 = ele ? ele.getBoundingClientRect().top : 0;
// 如果ref是在v-for中定义的,需如下获取
const top1 = ele ? ele[0].getBoundingClientRect().top : 0;
// 获取滚动条的位置
const top2 = document.body.scrollTop || document.documentElement.scrollTop;
// 计算元素距视窗的实际高度
const top = top1 + top2;
// 滚动到相应位置
window.scrollTo(0, top);
posted @ 2023-10-16 14:34  SangFall  阅读(109)  评论(0)    收藏  举报