I've been working on adding a scroll-to-top button to my website. After doing some research online, I came across this code snippet:
$('html, body').animate({scrollTop:0}, 'slow');
It's interesting that they are trying to scroll both the html
and body
elements. However, when I played around with the overflow: hidden
property on the body
tag, I realized that having a scrollbar for both html
and body
may not work as expected. Then I found an alternative method on MDN:
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
});
It's worth mentioning that the window
object is global and cannot be scrolled.
- Does the
window.scroll
function internally scroll thehtml
element or thebody
element? - Is it possible to scroll the
body
element at all? I experimented with usingoverflow: hidden
onhtml
and thenoverflow: scroll
onbody
, but couldn't get a scrollbar to show up.