2015-09-29 73 views
0

我试图复制我的鼠标单击和滑动操作以在手机或触摸屏上工作。我想知道是否有人可以帮助我在桌面和移动应用程序上使用此代码。这里是我的JavaScript和小提琴: 我试图做到这一点在普通的JavaScript,而无需使用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"; 
} 
} 

回答

0

我想我想通了。

document.touchstart不工作,我不得不使用:

var touchDown = 0; 
document.addEventListener("touchstart", start, false); 
document.addEventListener("touchend", end, false); 
function start() { 
++touchDown; 
} 
function end() { 
--touchDown; 
} 

这似乎是在这里工作! http://jsfiddle.net/Ltdgx363/5/