Skip to content

Instantly share code, notes, and snippets.

@Schniz
Last active December 16, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schniz/5388123 to your computer and use it in GitHub Desktop.
Save Schniz/5388123 to your computer and use it in GitHub Desktop.
Prevents mouse scrolling on the whole page, and just scrolls that awesome selected item. I don't remember where I found this code, but here it is, converted to jQuery's awesome stuff. example $("ul#awesome-but-long-list").mouseScroll(); returns jQuery $(this), for fluent access
/**
* Prevents mouse scrolling on the whole page, and just scrolls that awesome selected item.
* I don't remember where I found this code, but here it is, converted to jQuery's awesome stuff.
*
* @example $("ul#awesome-but-long-list").mouseScroll();
* @return jQuery $(this), for fluent access
*/
$.fn.mouseScroll = function () {
return $(this).bind('mousewheel DOMMouseScroll', function (e) {
var delta = e.wheelDelta || -e.detail || e.originalEvent.wheelDelta;
var scrollDelta = (delta < 0 ? 1 : -1) * 30;
// Check if thats an iScroll
if ($(this).data('iScroll-Object') instanceof iScroll) {
$(this).data('iScroll-Object').scrollTo(0, scrollDelta, 150, true);
} else { // Standard DOM
this.scrollTop += scrollDelta;
}
e.preventDefault();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment