» Javascript
(*) You can copy the code provided and paste it into the browser console of any open website. Then, press Enter to execute the code and test the smooth scroll to top functionality.
const scrollTopFunc = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollTopFunc);
window.scrollTo(0, c - c / 8);
}
};
scrollTopFunc();
» JQuery
const scrollTopFunc = () => {
$('html, body').animate({ scrollTop: 0 }, 800);
};
scrollTopFunc();