Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.wheelDelta returns undefined

So I'm trying to disable scrolling on my page when my lightbox opens, and I found this really usefull script that does exactly that. (http://jsfiddle.net/mrtsherman/eXQf3/3/), unfortunately, when I use it on my own page, it disabled scrolling in my lightbox as well. I started to debug the code with alerts only to find out that event.wheelDelta returns "undefined" on my page, while in the JSFiddle, it returns -120.

like image 382
CupOfTea696 Avatar asked Jan 16 '12 21:01

CupOfTea696


2 Answers

The event object in a jQuery event handler does not reflect the real event. wheelDelta is a non-standard event propertyIE and Opera, available through the originalEvent property of the jQuery event.

In jQuery 1.7+, the detail property is not available at the jQuery Event object. So, you should also use event.originalEvent.detail to for this property at the DOMMouseScroll event. This method is backwards-compatible with older jQuery versions.

event.originalEvent.wheelDelta 

Demo: http://jsfiddle.net/eXQf3/22/
See also: http://api.jquery.com/category/events/event-object/

like image 143
Rob W Avatar answered Sep 18 '22 20:09

Rob W


$.fn.wheel = function (callback) {     return this.each(function () {         $(this).on('mousewheel DOMMouseScroll', function (e) {             e.delta = null;             if (e.originalEvent) {                 if (e.originalEvent.wheelDelta) e.delta = e.originalEvent.wheelDelta / -40;                 if (e.originalEvent.deltaY) e.delta = e.originalEvent.deltaY;                 if (e.originalEvent.detail) e.delta = e.originalEvent.detail;             }              if (typeof callback == 'function') {                 callback.call(this, e);             }         });     }); }; 

and use like this:

$('body').wheel(function (e) {     e.preventDefault();     $('#myDiv').append($('<div>').text(e.delta)); }); 

big thx to @Mark for jquery-fying the function (:

like image 36
psycho brm Avatar answered Sep 17 '22 20:09

psycho brm