d4nyll / lethargy

Distinguish between scroll events initiated by the user, and those by inertial scrolling
http://d4nyll.github.io/lethargy/
MIT License
556 stars 47 forks source link

Initial up and initial down swipe always trigger twice.. #15

Closed katerlouis closed 7 years ago

katerlouis commented 7 years ago

.. no matter how long the timeout is. The issue occurs on trackpads (tested on different Macbooks)

After those initial up and down swipe it works perfectly across all tested devices.

Refreshed page: Console for inputs: "Down" > "Up" > "Down" > "Up"

00cba65d-0f2b-4bcd-ae28-066abfb300c9
var lethargy = new Lethargy(20, 40, 0.05); //  

// custom throttle 
var lethargyWait = false;
var lethargyTimeout = 500;

$(window).bind('mousewheel DOMMouseScroll wheel MozMousePixelScroll', function(e) {
    e.preventDefault()
    e.stopPropagation();

    var result = lethargy.check(e);

    if(result != false && lethargyWait != true) {

    console.log(result);

    if (result < 0) nextPanel();
    else if (result > 0) prevPanel();

    lethargyWait = true;
    setTimeout(function() {
        lethargyWait = false; 
    }, lethargyTimeout);

    }
});
d4nyll commented 7 years ago

Lethargy doesn't work for the first 15 events or so. This is because it must hold a cache of past events to determine whether the next event is likely to be an intent or not. If you see real carefully in the demo, the first 15 events would all be green, only when the initial 15-unit cache has been filled would lethargy work properly.

For normal mouse events, because a single scroll / swipe fires 10-20 events anyways, so the cache fills up quickly. For normal mouse events, this would not be a problem because they have no inertial scroll.

Your current set up has a stability value of 20, which actually extends that cache to ~40 events. If you decrease that back to around 8, it should not fire 2 events with your timeout function.

If it doesn't work, please post (screenshot) a profile of the scroll on your Macbook trackpad using this tool, similar to the image you see below:

image

katerlouis commented 7 years ago

Thank you so much. It works like a charm with stability 8. Awesome!

<3