Skip to content

Instantly share code, notes, and snippets.

@mbparvezme
Last active March 4, 2024 08:09
Show Gist options
  • Save mbparvezme/9a14690264e6fc66d92fa0c4ee8d49bc to your computer and use it in GitHub Desktop.
Save mbparvezme/9a14690264e6fc66d92fa0c4ee8d49bc to your computer and use it in GitHub Desktop.
// Button/Link: #backToTopBtn
document.getElementById('backToTopBtn').addEventListener('click', () => {
var start = window.pageYOffset, startTime = null
let step = (timestamp) => {
if (!startTime) startTime = timestamp
var progress = timestamp - startTime,
percentage = Math.min(progress / 1000, 1)
window.scrollTo(0, start * (1 - percentage))
if (progress < 1000) window.requestAnimationFrame(step)
}
window.requestAnimationFrame(step)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment