Daniel@0: // Daniel@0: // Prevent horizontal scroll for Back page in Mac 10.7+ Daniel@0: // Daniel@0: // Mac OSX Lion introduces a nasty behavior: when you are scrolling and Daniel@0: // the element (or its parents) are no longer scrollable, then horizontal Daniel@0: // scrolling with two fingers will trigger back page or next page. Daniel@0: // Daniel@0: // For now this plugin provides a way to prevent that behavior for Chrome Daniel@0: // in the case you're scrolling up or left where you can't scroll anymore, Daniel@0: // which triggers back/next page. Daniel@0: // Daniel@0: // Supported browsers: Mac OSX Chrome, Mac OSX Safari, Mac OSX Firefox Daniel@0: // On all other browsers this script won't do anything Daniel@0: // Daniel@0: // Depends on: jquery.mousewheel.js Daniel@0: // Daniel@0: // by Pablo Villalba for http://teambox.com Daniel@0: // Daniel@0: // Licensed under the MIT License Daniel@0: // Daniel@0: Daniel@0: (function ($) { Daniel@0: Daniel@0: // This code is only valid for Mac Daniel@0: if (!navigator.userAgent.match(/Macintosh/)) { Daniel@0: return; Daniel@0: } Daniel@0: Daniel@0: // Detect browsers Daniel@0: // http://stackoverflow.com/questions/5899783/detect-safari-using-jquery Daniel@0: var is_chrome = navigator.userAgent.indexOf('Chrome') > -1; Daniel@0: var is_safari = navigator.userAgent.indexOf("Safari") > -1; Daniel@0: var is_firefox = navigator.userAgent.indexOf('Firefox') > -1; Daniel@0: Daniel@0: // Handle scroll events in Chrome, Safari, and Firefox Daniel@0: if (is_chrome || is_safari || is_firefox) { Daniel@0: Daniel@0: // TODO: This only prevents scroll when reaching the topmost or leftmost Daniel@0: // positions of a container. It doesn't handle rightmost or bottom, Daniel@0: // and Lion scroll can be triggered by scrolling right (or bottom) and then Daniel@0: // scrolling left without raising your fingers from the scroll position. Daniel@0: $(window).mousewheel(function (e, d, x, y) { Daniel@0: Daniel@0: var prevent_left, prevent_up; Daniel@0: Daniel@0: // If none of the parents can be scrolled left when we try to scroll left Daniel@0: prevent_left = x < 0 && !_($(e.target).parents()).detect(function (el) { Daniel@0: return $(el).scrollLeft() > 0; Daniel@0: }); Daniel@0: Daniel@0: // If none of the parents can be scrolled up when we try to scroll up Daniel@0: prevent_up = y > 0 && !_($(e.target).parents()).detect(function (el) { Daniel@0: return $(el).scrollTop() > 0; Daniel@0: }); Daniel@0: Daniel@0: // Prevent futile scroll, which would trigger the Back/Next page event Daniel@0: if (prevent_left || prevent_up) { Daniel@0: e.preventDefault(); Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: } Daniel@0: Daniel@0: }(jQuery));