Created
March 2, 2011 07:21
-
-
Save mwbrooks/850593 to your computer and use it in GitHub Desktop.
code golf! 446 bytes minified (yui-compressor defaults)
This file contains 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(window, document) { | |
/** | |
* Do not use thumbs.js on touch-enabled devices | |
*/ | |
if (document.ontouchstart) return; | |
/** | |
* Map touch events to mouse events | |
*/ | |
var eventMap = { | |
'mousedown': 'touchstart', | |
'mouseup': 'touchend', | |
'mousemove': 'touchmove' | |
}, key, eventObject; | |
/** | |
* Fire touch events | |
* | |
* Monitor mouse events and fire a touch event on the | |
* object broadcasting the mouse event. This approach | |
* likely has poorer performance than hijacking addEventListener | |
* but it is a little more browser friendly. | |
*/ | |
for (key in eventMap) { | |
document.addEventListener(key, function(e) { | |
e.target.dispatchEvent(createTouchEvent(e)); | |
}, 0); | |
} | |
/** | |
* Utility function to create a touch event. | |
* | |
* @param name {String} of the event | |
* @return event {Object} | |
*/ | |
function createTouchEvent(e) { | |
eventObject = document.createEvent('MouseEvents'); | |
eventObject.initMouseEvent( | |
eventMap[e.type], | |
e.cancelBubble, | |
e.cancelable, | |
e.view, | |
e.detail, | |
e.screenX, | |
e.screenY, | |
e.clientX, | |
e.clientY, | |
e.ctrlKey, | |
e.altKey, | |
e.shiftKey, | |
e.metaKey, | |
e.button, | |
e.relatedTarget | |
); | |
return eventObject; | |
}; | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well, if you want, lets try to get thumbs.js working with GloveBox. Would be a good reason to get touch properties in thumbs.js. There is also an issue around touchmove. At the moment it's mapped to mousemove, but a touch move is really a mousedown + mousemove.
Being concerned about
if (typeof document.ontouchstart != 'undefined') return;
is fair. It works in the few browsers that I have tested on. I liketry { document.createEvent("TouchEvent"); } catch(e){return;}
. It feels more concrete.