问题
I'm trying to duplicate my mouse click and swipe action to work on the phone or touch screen. I'm wondering if anyone could assist me with making this code work on both desktop and mobile applications. Here is my javascript and fiddle: I'm trying to do this in plain javascript without having to use jquery
http://jsfiddle.net/Ltdgx363/2/
obj=document.getElementsByTagName("object");
var mouseDown = 0;
document.onmousedown = function() {
++mouseDown;
}
document.onmouseup = function() {
--mouseDown;
}
var touchDown = 0;
document.touchstart = function() {
++touchDown;
}
document.touchend = function() {
--touchDown;
}
for(i = 0; i < obj.length; i++) {
obj[i].addEventListener("mouseover", colorred ,false);
obj[i].addEventListener("touchmove", colorred ,false);
}
function colorred(){
if(mouseDown>0|touchDown>0){
this.className = "red";
}
}
回答1:
I think I figured it out.
document.touchstart doesn't work I had to use:
var touchDown = 0;
document.addEventListener("touchstart", start, false);
document.addEventListener("touchend", end, false);
function start() {
++touchDown;
}
function end() {
--touchDown;
}
It seems to be working here! http://jsfiddle.net/Ltdgx363/5/
来源:https://stackoverflow.com/questions/32850128/creating-a-mouse-and-swipe-event-for-mobile-applications