How to call a function, when page is scrolled to certain element?

can you tell me why my code doesn’t work. I want to call a function when page is scrolled to certain element. (I would like to find solution in vanilla js without using jQuery)

const numberContainer = document.querySelector('.numbers');

window.addEventListener('scroll', () => {
    const scrolled = window.scrollY;
    const elementPosition = numberContainer.scrollY;
    
    if(scrolled == elementPosition){
        counter();
    }
})

2 thoughts on “How to call a function, when page is scrolled to certain element?”

Leave a Comment