2

Firefox's new smooth scrolling feature causes the scroll callback to trigger at each step in the animation.

DEMO in FF and Chrome to see the difference

Is there any way to have it so that it

  1. Only fires one event when the page has finished scrolling
  2. Make the page scroll abruptly like it does in Chrome
qwertymk
  • 34,200
  • 28
  • 121
  • 184

2 Answers2

3

Try this:

function throttle( fn, timeout ) {
    var tid = 0;
    return function() {
        clearTimeout( tid );
        var args = [].slice.call( arguments ),
            ctx = this;

        tid = setTimeout( function() {
            fn.apply( ctx, args );
        }, timeout );
    };
}

$(window).on("scroll", throttle( function() {
    $('div').eq(0).append('scroll happened');
}, 100));

It will only fire the scroll once no scroll has happened in 100 milliseconds.

http://jsfiddle.net/NLGHS/1/

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • nice, but it feels more hacky than I want. Also it only executes 100 milliseconds after the scroll. – qwertymk Jun 12 '12 at 12:12
  • There is no way to predict when user has stopped scrolling except to predict a certain time like 100ms. Even if you had something like this in a "non-hacky" package, the implementation would use such method under the covers for sure. For example, "doubleclick" is merely 2 clicks where the time between the clicks is `x` milliseconds, for example 300. – Esailija Jun 12 '12 at 12:14
0

It depends on your purpose. I'm taking a guess based on your code that you would like to trigger an animation as the user scrolls down the page, a bit like Ben the Bodyguard: http://benthebodyguard.com/index.php

To achieve this, you tie the animation to the position in the page. You can get the current scroll position from the event object that is passed to the scroll method. You'll then need to do some maths to determine if the current scroll position has changed enough to trigger the next animation.

superluminary
  • 47,086
  • 25
  • 151
  • 148