Last active
December 18, 2015 13:59
-
-
Save rhyzx/5794372 to your computer and use it in GitHub Desktop.
fast click for touch-based(mobile) browsers.
require jQuery.
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 () { | |
if ('ontouchstart' in document) { | |
var touched, startX, startY | |
$(document) | |
.on('touchstart', function (evt) { | |
touched = evt.target | |
var touch = evt.originalEvent.touches[0] | |
startX = touch.clientX | |
startY = touch.clientY | |
}) | |
.on('touchmove', function (evt) { | |
var touch = evt.originalEvent.touches[0] | |
if (Math.abs(startX - touch.clientX) > 10 || Math.abs(startY - touch.clientY) > 10) | |
touched = null | |
}) | |
.on('touchend', function (evt) { | |
if (touched) $(touched).trigger('tap') | |
}) | |
} else { // fallback for touch-unsupported browsers | |
$(document).on('click', function (evt) { | |
$(evt.target).trigger('tap') | |
}) | |
} | |
}() |
TODO
use native event model
var evt = document.createEvent('HTMLEvents')
evt.initEvent('tap', true, true)
target.dispatchEvent(evt)
// or CustomEvent()?
challenge
ie6 dont support custom event
http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/ hack
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage