osx inertia
hi!
when using a trackpad or a magic mouse multiple events are triggered for every touch-scroll, sometimes taking as long as 1-2 seconds to stop, depending the scrolling "speed" and due to the inertia setting on mac osx.
I'm using mousewheel in a scenario where I move through news items on mousewheel and ideally I only want to move by on one each mousewheel action on windows, one each touch-scroll gesture on a mac trackpad.
The multiple events triggered make this impossible to do. I've tried unbinding and rebinding but as soon as I rebind the events triggered by the touch action before the unbind keep getting detected. I've tried filtering the delta to only scroll if it's between 2 values but its making the scroll very unreliable. Rebinding mousewheel only after the inertia events stop is also an impossibility because it would mean only allowing one scroll every two seconds.
do you have any ideas on how to overcome this?
Underscore's debounce function is a good solution here. http://documentcloud.github.com/underscore/#debounce
You would set up your callback passing the 'immediate' parameter like this:
myCallback = _.debounce(myCallback, 200, true);
I was having the exact same problem here. I've been trying to manipulate/lower the delta (by multiplying it with some negative threshold value) to achieve some "slower" scrolling and to deal with osx scroll inertia. Now looking into underscore's (actually using lo-dash.js) debounce function I was able to make the scrolling more controllable. Thanks for pointing that out!
I found out that the magic number here was 40. Let me explain myself. It seems that with the trackpad (probably with the magic mouse too) the delta get increased 40 times. And it stays that way even if you take back your normal mouse and scroll with it later. So what I did, simply :
b.mousewheel(function(evt,delta,deltaX,deltaY){
if(Math.abs(deltaY)>=40)
deltaY/=40;
if(Math.abs(deltaX)>=40)
deltaX/=40;
//Do your stuff here.
});
Any recent, all-encompassing solution w/ the latest version, that will work when the Apple Magic Mouse is present, but also if not, on Windows, tablets and phones (iOS and otherwise)?
Some of these ideas seem to help a bit, or in certain circumstances, but then fall apart on others. Can't seem to find a pattern...
Although has some interesting possibilities: http://stackoverflow.com/questions/17798091/osx-inertia-scrolling-causing-mousewheel-js-to-register-multiple-mousewheel-even
Yet also, not quite THE answer.
The issue @NeekGerd brought up is actually related to #66.
I added some functionality that can help with this issue in the 3.2.x branch.
$(...).on('mousewheel', { mousewheel: { behavior: 'throttle', delay: 100 } }, function(event) { ... });
or
$(...).on('mousewheel', { mousewheel: { behavior: 'debounce', delay: 100 } }, function(event) { ... });
I went ahead and bumped this to a 4.0.x branch instead of the previously mentioned 3.2.x branch because the level of changes are pretty extreme.
In experimenting with this feature I noticed that it might be desirable to prevent the default behavior for all the mouse wheel events that are essentially ignored due to the throttling. In addition I was thinking about utilizing this concept of putting the settings in the data param for a few other nice-to-have features as well... So I think I might change up the setting names a little based on how that work goes.
Also thinking about alternative ways to add this behavior. Possibly just expose a factory-style method that can build the desired delayed handler.
Another update on this. You can now pass the following as settings for the mousewheel event:
{ mousewheel: { debounce: { delay: 200 } } { mousewheel: { throttle: { delay: 200 } }
Or you can roll with the defaults by just setting to true like this:
{ mousewheel: { debounce: true } } { mousewheel: { throttle: true } }
Be default it will not prevent the default nor stop the propagation but you can do both like this:
{ mousewheel: { debounce: { preventDefault: true } } { mousewheel: { throttle: { preventDefault: true } }
There is another feature that I added as well... intent. It work similar to the popular hoverIntent plugin.
{ mousewheel: { intent: { delay: 300, sensitivity: 7 } }
Or use the defaults and just pass true:
{ mousewheel: { intent: true } }
You can even use both the intent feature and the throttle/debounce but only preventDefault once the user has expressed intent to use the mousewheel on that particular object.
{ mousewheel: { intent: true, throttle: { preventDefault: true } }
I'm still doing lots of testing and exploring of these features/ideas and they very well could change again. All of this work is happening in the 4.0.x branch.
I just landed leading, trailing, and maxDelay options for throttle/debounce settings in the 4.0.x branch. The maxDelay setting only applies to debounce. By default leading is false and trailing is true for debounce while both leading and trailing are true for throttle.
awesome man. keep up the good work.
Great update on this @brandonaaron . debounce stops the execution for given seconds. But what does the throttle do then ? Can you create a simple demo, where I can see how I can use those and test ?
@jashwant throttle caches the event action and do one at a time, by the delay you specified. debounce doesn't cache, executes only 1 and throw away the subsequent actions until the next release (timeout of the delay specified)
I'm still having issues controlling macosx mousewheel event. I can't use debounce feature because it waits the specified ms to start, I need it to start immediatelly and only allow to scroll again after some time.
I'm able to do that normally on Windows and Linux (any browser) with my own code. On MacOSX feels erratic, some times it works, but with a "strong" mousewheel, it executes the event again before the time. No idea why, how or how to stop it.
Here's the code:
var enableRun = true;
var runSections = function(delta) {
if(enableRun) {
if(delta < 0) {
// up
self.sly.prev();
} else if(delta > 0) {
// down
self.sly.next();
}
enableRun = false;
setTimeout(function() {
enableRun = true;
}, 800);
}
};
var homeScroll = function(event) {
var delta = event.deltaY;
if(self.sly.initialized) {
var items = self.sly.items;
var current = self.sly.rel;
var isLastItem = (items.length - 1 === current.lastItem);
if(isLastItem) {
if(delta < 0) {
runSections(delta);
event.stopPropagation();
event.preventDefault();
}
if(!enableRun) {
event.stopPropagation();
event.preventDefault();
}
} else {
runSections(delta);
event.stopPropagation();
event.preventDefault();
}
}
};
$(window).on('mousewheel', homeScroll);
@miguelpeixe you can turn on a leading debounce behavior. By default leading is false and trailing is true for debounce. There is also a maxDelay option for the debounce behavior.
$(...).on('mousewheel', { mousewheel: { debounce: { leading: true, trailing: false, maxDelay: 500 } }, fn);
Related code for the leading/trailing default settings: https://github.com/brandonaaron/jquery-mousewheel/blob/4.0.x/jquery.mousewheel.js#L206-L207
Thanks @brandonaaron, what does maxDelay do?
If the event is debounced for maxDelay milliseconds it will force a fire of the event.
@miguelpeixe Did the additional settings for debounce work for your particular use-case?
@brandonaaron actually no, my client decided to disable our custom wheel feature but I'm still very interested in solving this out. Such a simple thing, causing so much stress, that's weird.
Do you know what I'm trying to do? if you do and want to solve this, would be nice to have some help!
In my case, I don't really want to force a wheel event, I just need to control it to happen only on the given ms (if the user wants to, with leading turned on). I couldn't manage to do that with the available settings. The closest I got was with the code I previously posted here (worked on every os and browser, except macosx).
Hi, I'm having the same issue of miguelpeixe. I can control events on all browsers on windows; on mac Os x I'm using a magic mouse or a trackpad: on firefox everything goes fine, while on chrome (espcially with strong wheel actions) things go out of control and preventDefault seems to be ignored... the code is pretty similar to miguelpeixe's one, I can post it if you think it can be useful...