Element.scrollIntoView()指定元素自动滚动到可视区域

1,385 阅读1分钟

示例:

var element = document.getElementById("box");

element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({behavior: "smooth", block: "end", inline: "nearest"});

在vue中使用,注意方法再mounted中调用

this.$nextTick(()=>{     
    //水平滑动到指定元素
     document.querySelector("#box").scrollIntoView()
})

API:developer.mozilla.org/zh-CN/docs/…