Auto scroll to a specify element every time open webpage by Javascript

Auto scroll to a specify element by Javascript

We have an example webpage like this:

The browser will auto scroll to element that it has id #positionElement every time we open webpage:

const positionElement = document.querySelector('#positionElement')
const topPositionElement = positionElement.getBoundingClientRect().top + window.pageYOffset

  top: topPositionElement, // (*)
  behavior: 'smooth' // (**)

(*) Property top scroll to top of element
(**)behavior 'smooth' will make scroll smoothly

© 2019 4codev

Created with love by Sil.