Is it possible to use element.scrollIntoView and a "scroll" event listener together?
What i want to do with my code is check if the user scrolled by a certain element, if so, then the page should automatically scroll smoothly to the next section, to do this i tried to use a scroll listener and element.scrollIntoView with behavior smooth, and doing that causes a loop that makes the scroll go really slooow.
I also tried to remove the listener once the function executes, but that doenst work because i need to constantly execute that same function to check the scroll position, testing with console.log made me see that removing the listener makes it execute only once.
Here is the function:
let scrollBox = document.getElementById("scrollbox")
function onScroll(event){
window.removeEventListener("scroll", onScroll);
elementTop = scrollBox.getBoundingClientRect().top;
if(elementTop < 800 && elementTop > 600)
{
divStop.scrollIntoView({block: "end", behavior: "smooth"});
}
event.preventDefault();
};
window.addEventListener("scroll", onScroll)
So, how should i proceed? is there a way to use then both together that i am missing, if not then how can i complete my objective/idea ? Please help.