A memo about how to execute JavaScript programs/functions on each mouse scroll in JavaScript. Use document.addEventListener and jQuery’s .on/.off and is(“:visible”). Here are some examples.
console.log on each scroll:
function scrollfunc(){
console.log('y')
}
document.addEventListener("scroll", scrollfunc);
// or
document.addEventListener("scroll", function(){ console.log('y') ; });
An example of executing only once when an element is visible:
$(window).on('scroll',function() {
if ($(document.body).is(":visible")) {
console.log('y') ;
$(window).off('scroll');
} else {
// do nothing
}
});