Use scrollTo to scroll smoothly to the top

There is a back button in the lower right corner of the project page, click it to scroll to the top.
I thought about using animation at first, but then I thought that if animation is used, the scrolling time will be fixed, which will cause the scrolling to be fast and slow.
So is there a way to make the page scroll smoothly to the top?
The answer is to use the window.scrollTo(options) method. Options is an object with three attributes:
top is equivalent to y-coord
left is equivalent to x-coord
behavior type String, which represents the scrolling behavior and supports parameters smooth (smooth scrolling), instant ( Instant scrolling), the default value is auto (equivalent to instant),
here you want to scroll smoothly to the top, so use the smooth parameter

window.scrollTo({
  top:0,
  behavior:'smooth'
})

Guess you like

Origin blog.csdn.net/weixin_53841730/article/details/127667366