JavaScript allows you to simply return to the top of a webpage. You can refer to the fact that many webpages place a "Back to Top" button at the bottom, especially those without navigation at the bottom of the page, this will help visitors find the navigation again or review the advertisement (really nice ). As JavaScript has become more and more widely used in recent years, and the sliding effect is everywhere, I followed suit and made the function of returning to the top into a sliding effect. Later, in order to better fit the physical characteristics, the system made the sliding effect of deceleration.
Let's talk about the principle first. We will get the distance from the scroll bar to the top of the page, move it up to a certain distance, and then get the distance from the scroll bar to the top of the page, move up a certain distance (a little smaller than the previous one); and so on...
The Code is as follows: