Skip to content

Instantly share code, notes, and snippets.

@attenzione
Created May 10, 2014 15:35
Show Gist options
  • Save attenzione/a92d61ab257c2e2ad4f9 to your computer and use it in GitHub Desktop.
Save attenzione/a92d61ab257c2e2ad4f9 to your computer and use it in GitHub Desktop.
Normalize mousewheel event across browsers
// 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