There is such a code. I need to execute the js code when the page is scrolled by 50% 1 time.
But my code is executed every time I move.
Help me fix it
document.addEventListener("scroll", function() {
const height = window.scrollY / (document.documentElement.scrollHeight - document.documentElement.clientHeight)
if ( height > 0.5 ) {
console.log('End 50');
}
})
I tried return 0;
and break
2
Answers
Thanks! Although this part doesn't work correctly for me, I was able to write working code for myself.:
First, get the page height:
Now, get 50% of it:
Now listen to the scroll event:
Note: This approach may not work if the page has dynamically loaded content. Because the page height can increase as the user scrolls down the page, so it is difficult to determine the full page height.