d
Amit DhamuSoftware Engineer
 

Smooth Scroll To Top

1 minute read 00000 views
$('#backToTop').on('click', function () {
  $('html, body').animate({ scrollTop: 0 }, 'fast')
})

Without jQuery

The below has a fallback which doesn't smooth scroll to the top if the behaviour property is not supported.

const scrollToTop = () => {
  try {
    window.scrollTo({
      top: 0,
      left: 0,
      behavior: 'smooth',
    })
  } catch (error) {
    window.scrollTo(0, 0)
  }
}