在Vue中,有三种方式可以实现页面滑动至指定位置
方法1:
//先获取目标位置距离
mounted() {
this.$nextTick(() => {
setTimeout(() => {
let targetbox= document.getElementById('targetbox');
this.target= targetbox.offsetTop;
})
})
}
//再滑动指定距离
document.body.scrollTop = this.target;
js
方法2:
this.$nextTick(() => {
this.$refs.DOM.scrollTo(0,200);
this.$refs.result.scrollIntoView({ behavior: "smooth" });
})
js
方法3:
document.getElementById("target").scrollIntoView();
js