19

If i want to bind an event on page scrolling i can use scroll();.

But how to fire when scroll() is ended up?

I would like to reproduce this:

   $(window).scroll(function(){
    //do somenthing
    });

    $(window).scrollSTOPPED(function(){  //--> when i'm scrolling then i stop to scrolling (so NOT when page scrollbar is at the end top or bottom :)
    //do somenthing else
    });

any ideas?

2

4 Answers 4

46

tiny jquery way

$.fn.scrollStopped = function(callback) {
  var that = this, $this = $(that);
  $this.scroll(function(ev) {
    clearTimeout($this.data('scrollTimeout'));
    $this.data('scrollTimeout', setTimeout(callback.bind(that), 250, ev));
  });
};

After 250 ms from the last scroll event, this will invoke the "scrollStopped" callback.

http://jsfiddle.net/wtRrV/256/

lodash (even smaller)

function onScrollStopped(domElement, callback) {
  domElement.addEventListener('scroll', _.debounce(callback, 250));
}

http://jsfiddle.net/hotw1o2j/

pure js (technically the smallest)

function onScrollStopped(domElement, callback, timeout = 250) {
  domElement.addEventListener('scroll', () => {
    clearTimeout(callback.timeout);
    callback.timeout = setTimeout(callback, timeout);
  });
}

https://jsfiddle.net/kpsxdcv8/15/

strange fact

clearTimeout and clearInterval params don't have to be defined and can even be wrong types or even omitted.

http://jsfiddle.net/2w5zLwvx/

Sign up to request clarification or add additional context in comments.

1 Comment

can you provide just a little jsfiddle i'm not sure if i'm using this rightly
2

the event itself doesn't exist as scroll is a single event fired everytime the user scrolls by a certain increment.

What you can do however is emulate the event.

Credit to James Padolsey for this, lifted from his webpage:. Read it here to fully understand the code and how it is implemented.

http://james.padolsey.com/javascript/special-scroll-events-for-jquery/

(function(){

var special = jQuery.event.special,
    uid1 = 'D' + (+new Date()),
    uid2 = 'D' + (+new Date() + 1);

special.scrollstart = {
    setup: function() {

        var timer,
            handler =  function(evt) {

                var _self = this,
                    _args = arguments;

                if (timer) {
                    clearTimeout(timer);
                } else {
                    evt.type = 'scrollstart';
                    jQuery.event.handle.apply(_self, _args);
                }

                timer = setTimeout( function(){
                    timer = null;
                }, special.scrollstop.latency);

            };

        jQuery(this).bind('scroll', handler).data(uid1, handler);

    },
    teardown: function(){
        jQuery(this).unbind( 'scroll', jQuery(this).data(uid1) );
    }
};

special.scrollstop = {
    latency: 300,
    setup: function() {

        var timer,
                handler = function(evt) {

                var _self = this,
                    _args = arguments;

                if (timer) {
                    clearTimeout(timer);
                }

                timer = setTimeout( function(){

                    timer = null;
                    evt.type = 'scrollstop';
                    jQuery.event.handle.apply(_self, _args);

                }, special.scrollstop.latency);

            };

        jQuery(this).bind('scroll', handler).data(uid2, handler);

    },
    teardown: function() {
        jQuery(this).unbind( 'scroll', jQuery(this).data(uid2) );
    }
};   })();

Probably worth noting that there are several questions related to yours, so this may be a possible duplication. e.g. Javascript: do an action after user is done scrolling and Fire event after scrollling scrollbars or mousewheel with javascript

1 Comment

Since one of the answers of the second question you linked to also mentions the article you mentioned, it's certainly a duplicate and you should flag / vote to close it as such.
0

You can verify if window.scrollY == 0

$(window).scroll(function(){
  if (window.scrollY == 0) {
    //...
  }
});

But this event will be fired at every scroll.

5 Comments

So the code will be executed when the page was scrolled to the very top, not when the scrolling ends.
yep this is not related to my question
@FelixKling I got the checkmark on this one but it could easily be confused given your wording 'ended up' and that he is not from US. Marking him down isn't the right thing to do on this one as he has the chops.
@donotusetabtodigitthisnick did you down vote him? Sorry Felix.
@zipstory.com no i think not, sincerily do not remember, but i don't think
0

I prefer to be able to listen on a event. This is what I do:

The jquery plugin:

+function(jQuery){
        var scrollStopEventEmitter = function(element, jQuery) {
            this.scrollTimeOut = false;
            this.element       = element;
            jQuery(element).on('scroll', $.proxy(this.onScroll, this));
        }

        scrollStopEventEmitter.prototype.onScroll = function() 
        {
            if (this.scrollTimeOut != false) {
              clearTimeout(this.scrollTimeOut);
            }

            var context = this;

            this.scrollTimeOut = setTimeout(function(){ context.onScrollStop()}, 250);
        }

        scrollStopEventEmitter.prototype.onScrollStop = function() 
        {
            this.element.trigger('scrollStop');
        }

        jQuery.fn.scrollStopEventEmitter = function(jQuery) {   
            return new scrollStopEventEmitter(this, jQuery);
        };

    }($);

In this case, window will now trigger scrollStop event

$(window).scrollStopEventEmitter($);

Now I can listen on scrollStop

$(window).on('scrollStop',function(){
        // code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.