【发布时间】:2021-04-04 19:16:14
【问题描述】:
我需要一个纯 HTML 解决方案来滚动到我页面上的某个部分。我通常这样做:
html {
scroll-behavior: smooth;
}
#section1 {
background: blue;
height: 500px;
}
#section2 {
background: red;
height: 500px;
}
<a href="#section2">Section 2</a>
<section id="section1"></section>
<section id="section2"></section>
但是,一旦我在该页面的某处添加自动滚动 div,这将停止工作。
function scroll() {
document.getElementById("autoScroll").scrollBy(2, 2);
setTimeout(scroll, 30);
}
scroll();
html {
scroll-behavior: smooth;
}
#section1 {
background: blue;
height: 500px;
}
#section2 {
background: red;
height: 500px;
}
#autoScroll {
height: 200px;
overflow-y: scroll;
}
#content {
height: 500px;
background: green;
}
<a href="#section2">Section 2</a>
<section id="section1"></section>
<section id="section2">
<div id="autoScroll">
<div id="content"></div>
</div>
</section>
这似乎是 Chrome 独有的问题。至少它在 Firefox 中运行良好。我是否也需要使用 Javascript scrollIntoView() 在 Chrome 中实现此效果?还是我缺少一些 HTML 属性?
【问题讨论】:
-
你预计会发生什么?
-
我认为点击链接时页面仍会滚动到
section2。 -
我认为这是 HTML 结构的问题。看看我的回答。
标签: javascript html google-chrome scroll