Skip to content

Instantly share code, notes, and snippets.

@alunny
Forked from mwbrooks/thumbs.alunny.js
Created March 2, 2011 07:36
Show Gist options
  • Save alunny/850604 to your computer and use it in GitHub Desktop.
Save alunny/850604 to your computer and use it in GitHub Desktop.
455 bytes bitches
(function(window, document, notDefined) {
/**
* Do not use thumbs.js on touch-enabled devices
*/
if (document.ontouchstart!=notDefined) 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(eventMap[e.type], e));
}, 0);
}
/**
* Utility function to create a touch event.
*
* @param name {String} of the event
* @return event {Object}
*/
function createTouchEvent(name, e) {
eventObject = document.createEvent('MouseEvents');
eventObject.initMouseEvent(
name,
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