Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Created March 26, 2012 17:31

Revisions

  1. pamelafox created this gist Mar 26, 2012.
    43 changes: 43 additions & 0 deletions touch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    dom.each(function() {
    $(this).unbind('tap', callback);
    $(this).bind('tap', callback);

    $(this).bind('touchstart', function(e) {
    e.preventDefault();

    var item = e.currentTarget;
    if (ISTOUCHING) return;
    item.moved = false;
    ISTOUCHING = true;
    item.startX = e.touches[0].pageX;
    item.startY = e.touches[0].pageY;
    $(item).addClass('active');
    });

    $(this).bind('touchmove', function(e) {
    var item = e.currentTarget;
    if (Math.abs(e.touches[0].pageX - item.startX) > 10 ||
    Math.abs(e.touches[0].pageY - item.startY) > 10) {
    item.moved = true;
    $(item).removeClass('active');
    }
    });

    $(this).bind('touchend', function(e) {
    var item = e.currentTarget;
    ISTOUCHING = false;

    if(!item.moved) {
    //e.stopPropagation();
    //e.preventDefault();
    $(item).trigger('tap');
    }

    setTimeout(function() {
    $(item).removeClass('active');
    }, 1000);

    delete item.moved;
    });

    });