Answers for "javascript scroll to element with offset"

0

javascript scroll to element with offset

const element = document.getElementById('targetElement');
const offset = 45;
const bodyRect = document.body.getBoundingClientRect().top;
const elementRect = element.getBoundingClientRect().top;
const elementPosition = elementRect - bodyRect;
const offsetPosition = elementPosition - offset;

window.scrollTo({
  top: offsetPosition,
  behavior: 'smooth'
});
Posted by: Guest on September-28-2021

Code answers related to "javascript scroll to element with offset"

Code answers related to "Javascript"

Browse Popular Code Answers by Language