为移动应用程序创建鼠标和滑动事件 [英] creating a mouse and swipe event for mobile applications

查看:139
本文介绍了为移动应用程序创建鼠标和滑动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重复鼠标单击和滑动操作以在手机或触摸屏上工作.我想知道是否有人可以帮助我使此代码在台式机和移动应用程序上都能正常工作.这是我的JavaScript和小提琴: 我试图用普通的javascript做到这一点,而不必使用jquery

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";
}
}

推荐答案

我想我明白了.

document.touchstart无法正常工作,我必须使用:

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;
}

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

这篇关于为移动应用程序创建鼠标和滑动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆