public
Last active

This snippet maps mouse events and touch events onto one single event. This makes it easier in the code since you have to listen to only one event regardles whether it's desktop or mobile.

  • Download Gist
touchmouse.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
(function() {
/* == GLOBAL DECLERATIONS == */
TouchMouseEvent = {
DOWN: "touchmousedown",
UP: "touchmouseup",
MOVE: "touchmousemove"
}
/* == EVENT LISTENERS == */
var onMouseEvent = function(event) {
var type;
switch (event.type) {
case "mousedown": type = TouchMouseEvent.DOWN; break;
case "mouseup": type = TouchMouseEvent.UP; break;
case "mousemove": type = TouchMouseEvent.MOVE; break;
default:
return;
}
var touchMouseEvent = normalizeEvent(type, event, event.pageX, event.pageY);
$(event.target).trigger(touchMouseEvent);
}
var onTouchEvent = function(event) {
var type;
switch (event.type) {
case "touchstart": type = TouchMouseEvent.DOWN; break;
case "touchend": type = TouchMouseEvent.UP; break;
case "touchmove": type = TouchMouseEvent.MOVE; break;
default:
return;
}
var touch = event.originalEvent.touches[0];
var touchMouseEvent;
if (type == TouchMouseEvent.UP)
touchMouseEvent = normalizeEvent(type, event, null, null);
else
touchMouseEvent = normalizeEvent(type, event, touch.pageX, touch.pageY);
$(event.target).trigger(touchMouseEvent);
}
/* == NORMALIZE == */
var normalizeEvent = function(type, original, x, y) {
return $.Event(type, {
pageX: x,
pageY: y,
originalEvent: original
});
}
/* == LISTEN TO ORIGINAL EVENT == */
var jQueryDocument = $(document);
if ("ontouchstart" in window) {
jQueryDocument.on("touchstart", onTouchEvent);
jQueryDocument.on("touchmove", onTouchEvent);
jQueryDocument.on("touchend", onTouchEvent);
} else {
jQueryDocument.on("mousedown", onMouseEvent);
jQueryDocument.on("mouseup", onMouseEvent);
jQueryDocument.on("mousemove", onMouseEvent);
}
})();

You can simply do $(document).on('touchstart mousedown', doSomething); or if not using jQuery I have an abstraction for touch events as well, only thing I plan to change is to remove momentum from the ended dispatch (since it's easy to calculate and most times not needed): https://github.com/millermedeiros/MM_js_lib/blob/master/src/events/TouchController.js

nice - thanks for the review. That would make the sure would make the code less verbose. I pretty much only use this snippet when I need X/Y coordinates. I'd say the nicest part is not the "one event for all" - thing but rather the easy access for X and Y. One thing I might add is X/Y on touchend. It's really annoying that webkit does not support it. In my mind, that makes absolutly no sense.

Anyways. I have to rework this code anyways. Just made it a gist for easy access for an article I wrote. I didn't want to make a repo for 70 lines of code. :)

Thanks for feedback.

Hey!
This is a great piece of code. It works well, but I cannot seem to be able to make it work with the .live() function. I made sure that the TouchMouseEvent was fired, but for some reason the code in the live() function does not execute. I tried :
$('#MyElement').live('TouchMouseEvent', function(){...});
$('#MyElement').live('normalizeEvent', function(){...});
$('#MyElement').live('touchMouseEvent', function(){...});

None of them worked. I know that since jQuery 1.4, the live() function supports custom events, so I don't know why this would not work.
Any help would be appreciated.

@alexlaq Hey, I'm sorry - I haven't seen your comment until now (7 months later ... I know). You problably have figured it out by now but anyways:

You have to listen to one of the TouchMouseEvent like so:

$( "#id" ).live( TouchMouseEvent.DOWN, function( event ) {...} );

or

$( "#id" ).live( "touchmousedown", function( event ) {...} );

I hope that helps.

Thanks a lot Mathias for this amazing snippet, it's working very nice with my image crop process, because of this I have mobile devices enabled. :)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.