当用户单击div时,我正在使用以下javascript代码将div滚动到视图中。
<script>
function showDiv2() {
document.getElementById('about').style.display = "none";
document.getElementById('terms').style.display = "none";
document.getElementById('breaker').style.display = "block";
document.getElementById('contact_us').style.display = "block";
document.getElementById( 'contact_us' ).scrollIntoView('slow');
}
</script>
这段代码可以正常工作并将div滚动到视图中,但是没有任何效果,而不是将页面平滑滚动到我的div上,只是跳到div上。有什么办法可以使它平稳且缓慢地向下滚动到div?谢谢
最佳答案
根据Element.scrollIntoView()
documentation尝试以下操作:
element.scrollIntoView({block: "end", behavior: "smooth"});
但您必须记住,这是一项实验性功能,仅在Firefox中有效