javascript将div滚动到 View 中并具有平滑的滚动效果?

标签 javascript scroll

当用户单击 div 时,我使用以下 JavaScript 代码将 div 滚动到 View 中。

    <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滚动到 View 中,但没有任何效果,页面不是平滑地向下滚动到我的div,而是跳转到div。有没有办法让我顺利并缓慢地向下滚动到我的 div ?谢谢

最佳答案

根据Element.scrollIntoView() documentation试试这个:

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

但您必须记住,这是一项实验性功能,仅在 Firefox 中有效

关于javascript将div滚动到 View 中并具有平滑的滚动效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30895313/

相关文章:

javascript - 和/或滤波器阵列之间

javascript - Kendo UI [DropDownList] - 多个元素中的冲突

html - 如何避免带有 "#"的 iframe 中的链接使父 View 转到顶部

javascript - 当滚动条出现时播放 YouTube 视频

jquery - 固定菜单位置在滚动顶部

javascript - backbone.js - 每秒刷新一个集合?

javascript - 为 sencha 触摸应用程序创建双 slider

javascript - IE 中的 .text 更新问题

javascript - skrollr.js : Turn off or destroy skrollr at a given window width

css - HTML&CSS3*JavaScript 高级滚动