I am currently working on a script to detect when a specific element comes into view while scrolling.
const targetElement = document.getElementById('sidebar');
window.addEventListener('scroll', () => {
if (window.scrollY > targetElement.offsetTop) {
console.log('Element passed');
}
})
Here is the sample code you can refer to: https://codepen.io/RomanKomprs/pen/LMrPNJ
UPDATE: The initial code provided seems to have an issue. The condition for detecting when the scroll position surpasses the sidebar's offsetTop is not triggering as expected. Any pointers on what might be incorrect?