Capturing the “scroll down” event?

后端 未结 10 2217
闹比i
闹比i 2020-12-28 12:17

I\'m designing a very simple web page (HTML only), the only \"feature\" I want to implement is to do something when the user scrolls down the page, is there a way to capture

10条回答
  •  别那么骄傲
    2020-12-28 13:04

    Here is my solution in pure JavaScript, be careful the only problem is that it is executed a lot of times. Sorry for the spelling I use google translate, I am french ;)

    var scrollBefore = 0;
    
    window.addEventListener('scroll',function(e){
        const scrolled = window.scrollY;
    
        if(scrollBefore > scrolled){
            console.log("ScrollUP");
            scrollBefore = scrolled;
            //Desired action
        }else{
            scrollBefore = scrolled;
            console.log("ScrollDOWN");
            //Desired action
        }
    
    })

提交回复
热议问题