Skip to content

Instantly share code, notes, and snippets.

@roboshoes
Created April 13, 2012 10:43

Revisions

  1. roboshoes revised this gist Apr 13, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions touchmouse.js
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@
    switch (event.type) {
    case "touchstart": type = TouchMouseEvent.DOWN; break;
    case "touchend": type = TouchMouseEvent.UP; break;
    case "touchmove": type = TouchMouseEvent.MOVE; break;
    case "touchmove": type = TouchMouseEvent.MOVE; break;
    default:
    return;
    }
    @@ -58,9 +58,9 @@
    var jQueryDocument = $(document);

    if ("ontouchstart" in window) {
    jQueryDocument.on("touchstart", onTouchStart);
    jQueryDocument.on("touchmove", onTouchMove);
    jQueryDocument.on("touchend", onTouchEnd);
    jQueryDocument.on("touchstart", onTouchEvent);
    jQueryDocument.on("touchmove", onTouchEvent);
    jQueryDocument.on("touchend", onTouchEvent);
    } else {
    jQueryDocument.on("mousedown", onMouseEvent);
    jQueryDocument.on("mouseup", onMouseEvent);
  2. roboshoes created this gist Apr 13, 2012.
    70 changes: 70 additions & 0 deletions touchmouse.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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", onTouchStart);
    jQueryDocument.on("touchmove", onTouchMove);
    jQueryDocument.on("touchend", onTouchEnd);
    } else {
    jQueryDocument.on("mousedown", onMouseEvent);
    jQueryDocument.on("mouseup", onMouseEvent);
    jQueryDocument.on("mousemove", onMouseEvent);
    }

    })();