jQuery Tools Scrollable with Mousewheel - scroll ONE position and stop

后端 未结 4 856
我寻月下人不归
我寻月下人不归 2020-12-28 10:38

I am using bind/unbind for mousewheel scrolling, based on this SO response:

Jquery, unbinding mousewheel event, then rebinding it after actions are completed?

<
4条回答
  •  遥遥无期
    2020-12-28 11:06

    Take a look at this and see what you think. To start I wouldn't bind to mousewheel because Firefox uses DOMMouseScroll instead. This also uses a different delta handler which is the inverse of the one used by all other browsers. Instead bind to jQuery's scroll event which normalizes the behavior. You can track the last scrolltop position to determine whether user scrolled up or down.

    I am kind of making the assumption that you are animating between sections. Is there a callback function you can use to determine whether you should be scrolling? I created a global to track whether elements are currently animating. If so then we don't bother executing the function. Finally, I noticed that the callback for the scroll animation seems to trigger before the final scroll event occurs, so I actually had to use a setTimeout there. I didn't like it, but I couldn't figure out how else to get that callback to work properly.

    http://jsfiddle.net/Gj3Qy/

    var lastScrollTop = 0;
    var isDoingStuff = false;
    
    $(document).scroll(function(event) {
        //abandon 
        if(isDoingStuff) { return; }
    
        if ($(this).scrollTop() > lastScrollTop) {
            //console.log('down');
            $('.active').removeClass('active').next('div').addClass('active');
            isDoingStuff = true;
            $('html,body').animate( {scrollTop: $('.active').offset().top }, 1000, function() {
                setTimeout(function() {isDoingStuff = false;}, 100);
                console.log('off');
            });
        } else {
            //console.log('up');
        }
        lastScrollTop = $(this).scrollTop();
    })
    

提交回复
热议问题