【发布时间】:2021-09-07 12:41:59
【问题描述】:
我知道在 stackOverflow 上还有其他关于相同问题的问题(2016 年及更早版本),但我都通过了它们,但它们并没有解决我的问题。
滚动到视图工作,当我单击按钮时,页面会转到它应该转到的特定部分,但行为属性不起作用。
const alllinks = document.querySelectorAll("scroll:link");
alllinks.forEach(function (link) {
link.addEventListener("click", function (e) {
e.preventDefault();
const href = link.getAttribute("href");
// Scroll to top
if (href === "#") {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
//Scroll to other links
else if (href !== "#" && href.startsWith("#")) {
document.getElementById(href).scrollIntoView({
behavior: `smooth`,
});
}
});
});
以下是 html 对所有部分的看法。 (这里仅以一节为例)
<a class="scroll nav-list--item btn" href="#how">How it works</a>
<section class="section-more container" id='how'>...</section>
这就是所有需要的代码。
【问题讨论】:
-
it is suppose to go to but?嗯?请正确描述问题。此外,查看相应的 HTML 也会有所帮助
标签: javascript html css smooth-scrolling js-scrollintoview