javascript on window scroll

window.addEventListener('scroll', function() {
    console.log('scrolling');
});

// Alternative version
window.onscroll = function() {
    console.log('scrolling');
};

What's the equivalent of the following in plain JS?

$(window).scroll(function() { }); I'm also looking to animate scroll, e.g.:

$('html, body').animate({scrollTop:1750}, 'slow'); Should I be using

BETA Snippet explanation automatically generated by OpenAI:

Here is what the above code is doing:
1. On scrolling, the onscroll event is fired.
2. The function is defined.
3. The function is called.
4. The function is anonymous.
5. The function is assigned to the window.onscroll property.
6. The

Snippet By Tony Lea

·

Created October 19th, 2021

·

Report Snippet