Skip to content

Instantly share code, notes, and snippets.

@hiroy
Created January 25, 2012 17:41
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 hiroy/1677525 to your computer and use it in GitHub Desktop.
Save hiroy/1677525 to your computer and use it in GitHub Desktop.
taphold的なプラグイン
(function($) {
$.fn.taphold = function(tapholdCallback, duration) {
var timerId;
var toClick = true;
duration = duration || 720;
if (typeof window.ontouchstart !== "undefined"
&& typeof tapholdCallback == "function") {
$(this).css("webkitTouchCallout", "none");
$(this).on("touchstart", function(e) {
e.preventDefault();
timerId = setTimeout(function() {
setTimeout(function() {
toClick = false;
tapholdCallback(e);
}, 0);
timerId = null;
}, duration);
});
$(this).on("touchend touchmove touchcancel", function(e) {
e.preventDefault();
if (timerId) {
clearTimeout(timerId);
toClick = true;
}
if (toClick && $(this).onclick !== null) {
var elm = this;
setTimeout(function() { $(elm).click(); }, 0);
}
});
}
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment