Created
June 18, 2016 11:50
-
-
Save bantya/982557e77c4923325cb538bd5d7f3724 to your computer and use it in GitHub Desktop.
JavaScript: Event type, returns appropriate event type for device based on the parameter passed to it
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 returns appropriate event type for device based | |
// on the parameter passed to the function | |
var et = function(t) { | |
var msPointer = window.navigator.msPointerEnabled; | |
var isTouchDevice = (typeof(window.ontouchstart) != 'undefined') ? true : false; | |
var clickArray = ['c','click','mousedown','down','touchstart']; | |
var releaseArray = ['r','release','up','mouseup','touchend']; | |
var moveArray = ['m','move','mousemove','touchmove','drag']; | |
var eventType = ''; | |
if (clickArray.indexOf(t) > -1) { | |
eventType = (isTouchDevice) ? 'touchstart' : eventType; | |
eventType = (msPointer) ? 'MSPointerDown' : eventType; | |
eventType = (!eventType) ? 'mousedown' : eventType; | |
//return (isTouchDevice) ? 'touchstart' : 'mousedown'; | |
} | |
if (releaseArray.indexOf(t) > -1) { | |
eventType = (isTouchDevice) ? 'touchend' : eventType; | |
eventType = (msPointer) ? 'MSPointerUp' : eventType; | |
eventType = (!eventType) ? 'mouseup' : eventType; | |
//return (isTouchDevice) ? 'touchend' : 'mouseup'; | |
} | |
if (moveArray.indexOf(t) > -1) { | |
eventType = (isTouchDevice) ? 'touchmove' : eventType; | |
eventType = (msPointer) ? 'MSPointerMove' : eventType; | |
eventType = (!eventType) ? 'mousemove' : eventType; | |
//return (isTouchDevice) ? 'touchmove' : 'mousemove'; | |
} | |
if (eventType) return eventType; | |
alert('Please pass et() a paramater it can use.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment