Skip to content

Commit

Permalink
Don't clobber mouse offset properties if we don't adjust them
Browse files Browse the repository at this point in the history
Fixes #165
  • Loading branch information
dmethvin committed Aug 18, 2019
1 parent 4a707c2 commit fcb425e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
10 changes: 3 additions & 7 deletions jquery.mousewheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@
delta = 0,
deltaX = 0,
deltaY = 0,
absDelta = 0,
offsetX = 0,
offsetY = 0;
absDelta = 0;
event = $.event.fix( orgEvent );
event.type = "mousewheel";

Expand Down Expand Up @@ -191,16 +189,14 @@
// Normalise offsetX and offsetY properties
if ( special.settings.normalizeOffset && this.getBoundingClientRect ) {
var boundingRect = this.getBoundingClientRect();
offsetX = event.clientX - boundingRect.left;
offsetY = event.clientY - boundingRect.top;
event.offsetX = event.clientX - boundingRect.left;
event.offsetY = event.clientY - boundingRect.top;
}

// Add information to the event object
event.deltaX = deltaX;
event.deltaY = deltaY;
event.deltaFactor = lowestDelta;
event.offsetX = offsetX;
event.offsetY = offsetY;

// Go ahead and set deltaMode to 0 since we converted to pixels
// Although this is a little odd since we overwrite the deltaX/Y
Expand Down
2 changes: 1 addition & 1 deletion jquery.mousewheel.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,29 @@ QUnit.test( "natively triggered events", function( assert ) {

markup.remove();
} );

QUnit.test( "mouse event properties are passed through", function( assert ) {
assert.expect( 4 );

var markup = jQuery( "<p>wheelme</p>" ).appendTo( "body" );

markup.on( "mousewheel", function( e ) {
var org = e.originalEvent;
assert.equal( org.clientX, 342, "original event has clientX: " + org.clientX );
assert.equal( org.clientY, 301, "original event has clientY: " + org.clientY );
assert.ok( e.offsetX < org.clientX, "got plausible offsetX in the event: " + e.offsetX );
assert.ok( e.offsetY < org.clientY, "got plausible offsetY in the event: " + e.offsetY );
} );

// Not sure why this property is manipulating offsetX/Y but the behavior cannot
// change in a minor version so it will stay since it's set to true right now.
// For testing we just want to ensure that the properties get through.
var event1 = makeWheelEvent( 0, 2.2 );
event1.offsetX = 1;
event1.offsetY = 2;
event1.clientX = 342;
event1.clientY = 301;
markup[ 0 ].dispatchEvent( event1 );

markup.remove();
} );

0 comments on commit fcb425e

Please sign in to comment.