动态获得目标高度
<div ref="pronbit">
 <div>我的动态高度</div>
</div>
mounted(){
 window.addEventListener('scroll',this.handleScrollx,true)
},
methods: {
 handleScrollx() {
  console.log('滚动高度',window.pageYOffset)
  console.log('距离顶部高度',this.$refs.pronbit.getBoundingClientRect().top)
 },
}

获得高度后滚到到目标标签位置

let ttop = this.$refs.pronbit.getBoundingClientRect().top
console.log('top',ttop);
this.$nextTick(function () {
    window.scrollTo({'behavior': 'smooth', 'top': ttop})
})

记得最后加载,最好用延迟执行 setTimeout();

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐