creating a mouse and swipe event for mobile applications

夙愿已清 提交于 2019-12-12 03:59:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!