While listening to scrolling I want to trigger click event on an element only once. This event should scroll to an element. My code sofar:
window.addEventListener('scroll', () => {
window.onscroll = slideMenu;
if (window.scrollY > elementTarget.offsetTop) {
const scrolledPx = (window.scrollY - elementTarget.offsetTop);
if (scrolledPx > 100) {
const link = document.getElementById('2');
link.click();
link.removeEventListener('click'); // ????
}
}
}
The problem is that while I meet the condition scrolledPx > 100 - it keeps triggerring the link.click() event. Any ideas how to do it?