Created
May 10, 2014 15:35
-
-
Save attenzione/a92d61ab257c2e2ad4f9 to your computer and use it in GitHub Desktop.
Normalize mousewheel event across browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/questions/5527601/normalizing-mousewheel-speed-across-browsers | |
function normalize_mousewheel(e) { | |
var //o = e.originalEvent, | |
o = e, | |
d = o.detail, w = o.wheelDelta, | |
n = 225, n1 = n-1; | |
// Normalize delta | |
d = d ? w && (f = w/d) ? d/f : -d/1.35 : w/120; | |
// Quadratic scale if |d| > 1 | |
d = d < 1 ? d < -1 ? (-Math.pow(d, 2) - n1) / n : d : (Math.pow(d, 2) + n1) / n; | |
// Delta *should* not be greater than 2... | |
e.delta = Math.min(Math.max(d / 2, -1), 1); | |
} | |
/* Quick cross browser event attach - this is bad mmkay */ | |
var node = document.getElementById('scrollme'); | |
function listener(e) { | |
normalize_mousewheel(e); | |
node.scrollTop -= 40 * e.delta; | |
} | |
if ('onmousewheel' in node) { | |
node.onmousewheel = function(e) { | |
e = e || window.event; | |
listener(e); | |
} | |
} else { | |
node.addEventListener('DOMMouseScroll', listener) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment