Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Created December 13, 2011 01:10
Show Gist options
  • Save pamelafox/1469925 to your computer and use it in GitHub Desktop.
Save pamelafox/1469925 to your computer and use it in GitHub Desktop.
Click/touchstart wrapper
function addClickHandler(dom, callback, logThis) {
if (useTouchEvents()) {
dom.each(function() {
$(this).unbind('tap', callback);
$(this).bind('tap', callback);
$(this).bind('touchstart', function(e) {
var item = e.currentTarget;
if (ISTOUCHING) return;
item.moved = false;
ISTOUCHING = true;
window.setTimeout(function() {
if (item.moved === false) {
log('Adding class');
$(item).addClass('active');
}
}, 50);
});
$(this).bind('touchmove', function(e) {
var item = e.currentTarget;
item.moved = true;
$(item).removeClass('active');
});
$(this).bind('touchend', function(e) {
var item = e.currentTarget;
isTouching = false;
if(!item.moved) {
log('triggering tap on ' + $(item).text());
$(item).trigger('tap');
}
setTimeout(function() {
log('Removing class');
$(item).removeClass('active');
}, 1000);
delete item.moved;
});
});
} else {
dom.bind('click', callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment