0

How can I trigger just single event/function on scroll Up/Down with js/jquery.

This code is simple and nice for direction detect.

var lastScrollTop = 0;
$(window).scroll(function(event){
    var st = $(this).scrollTop();
        if (st > lastScrollTop){
            // downscroll code
        } else {
           // upscroll code
    }
    lastScrollTop = st;
});

How to unbind events after first call and bind after scroll is Finished? I would like to use it with a vertical fullscreen slider with TweenMax.js

Thanks!

user3245716
  • 33
  • 1
  • 6
  • how "pseuso" is your code? are you planning to use jQuery like $(window)? if yes, have a look at: http://stackoverflow.com/questions/4154952/jquery-bind-unbind-scroll-event-on-window additionally you can unbind specific handlers with http://api.jquery.com/unbind/ – ZPiDER Mar 25 '16 at 10:27
  • I think, I can use $(window). I tried to use bind/unbind in jQ but something went wrong. I've had some trouble with 'bind again' when scroll was finished and event was unbinded. I try again. Thanks! – user3245716 Mar 25 '16 at 11:06
  • Why do you want to unbind the event handler? This would mean your code only reacts to the single first scroll event that ever occurs on your page. Any scrolling after that will have no effect. In other words, the page functionality breaks immediately for the user. Does not sound very useful to me. – Tomalak Mar 25 '16 at 11:26
  • I try to do a slider. Every single scroll action from user = single slide. So, my idea was – on first call I shuld unbind scroll, and bind it after 0.5s, what is slide animation time. I tried to use setTimeout to bind. – user3245716 Mar 25 '16 at 11:40
  • if you have problems with binding/unbinding (i dont rule that out especially x-browser), you can also opt for "semaphoring" instead of unbinding. meaning: add an if (ThisIsFirst) {...} around the code in your function to prevent it from doing anything when you dont want to. – ZPiDER Apr 04 '16 at 10:08

0 Answers0