function smoothScrollTo(target, duration) { var timer, start, factor; var offset = window.pageYOffset, delta = target - window.pageYOffset; duration = duration || 1000; start = Date.now(); factor = 0; if (timer) { clearInterval(timer); } function step() { var y; factor = (Date.now() - start) / duration; if (factor >= 1) { clearInterval(timer); factor = 1; } y = factor * delta + offset; window.scrollBy(0, y - window.pageYOffset); } timer = setInterval(step, 10); return timer; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter