Created
January 25, 2012 17:41
-
-
Save hiroy/1677525 to your computer and use it in GitHub Desktop.
taphold的なプラグイン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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