How to determine scroll direction without actually scrolling

后端 未结 6 1352
走了就别回头了
走了就别回头了 2020-11-30 02:09

I am coding a page where the first time the user scrolls, it doesn\'t actually scroll the page down, instead it adds a class with a transition. I\'d like to detect when the

6条回答
  •  一向
    一向 (楼主)
    2020-11-30 03:07

    Try using e.wheelDelta

    var animationIsDone = false, scrollDirection = 0;
    
    function preventScroll(e) {
    
        e.preventDefault();
        e.stopPropagation();
    }
    
    $('body').on('mousewheel', function(e) {
    
        if (e.wheelDelta >= 0) {
            console.log('Scroll up'); //your scroll data here
        }
        else {
            console.log('Scroll down'); //your scroll data here
        }
        if (animationIsDone === false) {
            $("#main-header").removeClass("yellow-overlay").addClass("yellow-overlay-darker");
            $(".site-info").first().addClass("is-description-visible");
            preventScroll(e);
    
            setTimeout(function() {
                animationIsDone = true;
            }, 1000);
    
        }
    
    
    });
    

    Note: remember that MouseWheel is deprecated and not supported in FireFox

提交回复
热议问题