-
-
Save sakrist/a0df17e48a9a4c3c1674 to your computer and use it in GitHub Desktop.
createTouchEvent
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() { | |
var ua = /iPhone|iP[oa]d/.test(navigator.userAgent) ? 'iOS' : /Android/.test(navigator.userAgent) ? 'Android' : 'PC'; | |
document.addEventListener('DOMContentLoaded', function(event) { | |
if(ua === 'PC') { | |
return; | |
} | |
document.addEventListener('touchstart', function() { | |
console.log('Touch!'); | |
}, false); | |
var touchEvent = createTouchEvent(); | |
setTimeout(function() { | |
document.dispatchEvent(touchEvent); | |
}, 3000); | |
}, false); | |
function createTouchEvent(option) { | |
var option = option || {}; | |
var param = { | |
type: 'touchstart', | |
canBubble: true, | |
cancelable: true, | |
view: window, | |
detail: 0, | |
screenX: 0, | |
screenY: 0, | |
clientX: 0, | |
clientY: 0, | |
ctrlKey: false, | |
altKey: false, | |
shiftKey: false, | |
metaKey: false, | |
touches: 0, | |
targetTouches: 0, | |
changedTouches: 0, | |
scale: 0, | |
rotation: 0, | |
touchItem: 0 | |
}; | |
for(var i in param) { | |
if(param.hasOwnProperty(i)) { | |
param[i] = option[i] !== undefined ? option[i] : param[i]; | |
} | |
} | |
var event = document.createEvent('TouchEvent'); | |
if(ua === 'Android') { | |
event.initTouchEvent(param.touchItem, param.touchItem, param.touchItem, param.type, param.view, param.screenX, param.screenY, param.clientX, param.clientY, param.ctrlKey, param.altKey, param.shiftKey, param.metaKey); | |
} else { | |
event.initTouchEvent(param.type, param.canBubble, param.cancelable, param.view, param.detail, param.screenX, param.screenY, param.clientX, param.clientY, param.ctrlKey, param.altKey, param.shiftKey, param.metaKey, param.touches, param.targetTouches, param.changedTouches, param.scale, param.rotation); | |
} | |
return event; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment