js get element distance from top 1

js get element distance from top

const el = document.getElementById('id')
const elDistanceToTop = window.pageYOffset + el.getBoundingClientRect().top

Here is what the above code is Doing:
1. Get the element you want to scroll to
2. Get the distance from the top of the page to the element
3. Scroll to that element

Similar Posts