I need a pure HTML solution to scroll to a section on my page. I usually do it like this:
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>
However this stops working as soon as I add an auto-scrolling div somewhere on that page.
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>
This seems to be an Chrome only problem. At least it works fine in Firefox. Do I need to use Javascript scrollIntoView()
to achieve this effect in Chrome too? Or am I missing some HTML attributes?