Javascript mouse scroll execution

Execute on Mouse Scroll in JavaScript

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. An example of executing only once when an element is visible...

Shou Arisaka
1 min read
Oct 28, 2025

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
    }
});

Share this article

Shou Arisaka Oct 28, 2025

🔗 Copy Links