【问题标题】:How to smooth scroll to div on scroll using vanilla JS?如何使用 vanilla JS 在滚动时平滑滚动到 div?
【发布时间】:2020-11-05 13:14:54
【问题描述】:

我正在寻找一种在滚动时滚动到 div 的方法 - 仅在着陆 ID 上而不是在所有部分中寻找整页滚动。

我的 sn-p 中的 JS 已经在点击事件上工作,但不能让它在滚动上工作

document.getElementById('landing').addEventListener("scroll", function(event) {
  event.preventDefault();
  document.getElementById("main").scrollIntoView({ behavior: "smooth" });
});
#landing, #main {
  height:100vh;
}

#main {
  background: #EAEAEA;
}
<section id="landing"></section>

<section id="main"></section>

【问题讨论】:

标签: javascript


【解决方案1】:

我认为你的问题是#landing 没有溢出,所以它没有滚动。

我尝试向 div 中添加更多内容,它正在工作

看看这个:scroll() event not firing when attached to a div

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2013-10-01
    • 2014-07-12
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多