Skip to content
Advertisement

Implement element.scrollIntoView with scrollTo

How to implement

element.scrollIntoView({behavior: 'smooth', block: "end", inline: "nearest"})

using widnow.scrollTo({behavior: 'smooth', top: ??})?

I have created some Sandbox. I need to scroll exactly the same position as using scrollIntoView

Advertisement

Answer

Based on this answer

const targetEl = document.getElementById('target');
 
window.scrollTo({
  behavior: 'smooth', 
  top: targetEl.offsetTop + targetEl.clientHeight - window.innerHeight
})
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement