I have a pretty standard Flexslider instance I'm working on. Flexslider incorporates well with the jquery.mousewheel.js plugin, allowing slides to move on mousewheel events.
However, Mac OSX and others employ 'intertia' to scrolling effects, making it virtually impossible to scroll only one slide left or right. The general effect is that even the slightest scroll motion in these situations triggers multiple mousewheel events and scrolls the slider multiple images left or right. Obviously, this can't happen!
I think the solution is to use underscore.js - specifically the 'debounce' function. However, I'm relatively inexperienced in JS / jQuery and can't figure out how to implement it.
From underscore's documentation:
Debounce:
_.debounce(function, wait, [immediate])
Creates and returns a new debounced version of the passed function which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked. Useful for implementing behavior that should only happen after the input has stopped arriving. For example: rendering a preview of a Markdown comment, recalculating a layout after the window has stopped being resized, and so on.
Pass true for the immediate parameter to cause debounce to trigger the function on the leading instead of the trailing edge of the wait interval. Useful in circumstances like preventing accidental double-clicks on a "submit" button from firing a second time.
var lazyLayout = _.debounce(calculateLayout, 300); $(window).resize(lazyLayout);
Here's the code I'm using to initialize flexslider:
// MOUSEWHEEL FIX:
function debounceEffect() { // apply debounce to mousewheel here
}
// Initialize FlexSlider
$(window).load(function() {
$('div.flexslider').flexslider({
animation:'slide',
controlNav:true,
mousewheel:true,
pauseOnHover:true,
direction:'horizontal',
animationSpeed:500,
slideshowSpeed:5000,
after:debounceEffect
});
});
Unfortunately, I can't figure out how to apply the debounce function to the mousewheel event. I think I just don't understand the moving parts well enough.
Here is the relevant bit of code from the flexslider plugin:
// MOUSEWHEEL:
if (vars.mousewheel) {
slider.bind('mousewheel', function(event, delta, deltaX, deltaY) {
event.preventDefault();
var target = (delta < 0) ? slider.getTarget('next') : slider.getTarget('prev');
slider.flexAnimate(target, vars.pauseOnAction);
});
}
I can edit this to include some of the mousewheel plugin code as well if that's important, or you could check it out on github.
I would appreciate any help on this!