Skip to content

Instantly share code, notes, and snippets.

@StrangerPings
Forked from jeffreyiacono/lolrus.js
Created September 25, 2017 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StrangerPings/af997c5e339cf2e85a554a8cae0c4c05 to your computer and use it in GitHub Desktop.
Save StrangerPings/af997c5e339cf2e85a554a8cae0c4c05 to your computer and use it in GitHub Desktop.
simple normalization of jquery event so offsetX / offsetY can be used cross browsers
/**
* normalizeEvent
*
* Firefox does not implement offsetX, OffsetY, so we have to detect for this an
* manually calculate it ourselves using the pageX, pageY less the event
* target's left offset and right offset
*
* If using a browser that supports offsetX, OffsetY, just return the event,
* don't need to do anything
*/
var normalizeEvent = function(event) {
if(!event.offsetX) {
event.offsetX = (event.pageX - $(event.target).offset().left);
event.offsetY = (event.pageY - $(event.target).offset().top);
}
return event;
};
LOLrus.prototype.someMethodThatDependsOnEventPosition = function() {
$(this._someIternalHtmlDomEl)
.unbind('click')
.mousedown(_.bind(function(e) {
e = normalizeEvent(e);
this._somethingThatWantsCurrentMouseDownPosition(e.offsetX, e.offsetY);
}, this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment