-
-
Save alunny/850604 to your computer and use it in GitHub Desktop.
455 bytes bitches
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, 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