检测两个不同元素的触摸

时间:2012-01-22 09:53:41

标签: jquery dom cordova multi-touch

使用phonegap,jquery。 是否可以同时检测两个不同元素的触摸?

我正在制作需要按下两个图像(左按钮和右按钮)的东西 在触发另一个事件之前的同时。

以下不是详尽的代码列表,但应该提供对我想要实现的内容的深入了解

<head>

<script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jq.js"></script>
<script type="text/javascript" src="jquery.stopwatch.js"></script>

<script>
    var w = new Stopwatch(updatedisplay, 50);
    var onoff = false;

    $("#button-left, #button-right").unbind("touch",begin);

    function begin() {
        onoff = true;
        w.start();
    }

    function end() {
        w.stop();
        onoff = false;
    }       

    function updatedisplay(watch) {
        var mills = parseInt(watch.getElapsed().milliseconds/10);
        if(mills<10) {
            millis = '0'+mills;
        } else {
            millis = mills;
        }
        document.getElementById('counterValue').innerHTML = watch.toString() + "." + millis;
    }

    function reset() {
        w.stop();
        onoff = false;
        document.getElementById('counterValue').innerHTML = "00:00:00.00";
    }
</script>
</head>

<body onload="onBodyLoad()">
  <div id="counterValue">
    00:00:00.00
  </div>
  <div id="button-left"></div>
  <div id="button-right"></div> 
</body>

:::::创造了这个修复:::::一个可怜的但是它的工作非常好:::::

我找到了另一种方法,现在这可能有点难看......但它运作良好......

    $(document).ready(function() {                          
       document.getElementById('button-left').addEventListener('touchstart', nofunc(e), false);
       document.getElementById('button-right').addEventListener('touchstart', nofunc(e), false);
    });

    function vala(val) {
        av = val;
        checkvals();
    }
    function valb(val) {
        bv = val;
        checkvals();
    }
    function checkvals() {
        if(av == 1 && bv == 1) {
            if(onoff == false) {
                begin();
            } else {
                end();
            }
        }
    }

   <div id="button-left" ontouchstart="vala(1)" ontouchend="vala(0)">&nbsp;</div>
   <div id="button-right" ontouchstart="valb(1)" ontouchend="valb(0)">&nbsp;</div> 

2 个答案:

答案 0 :(得分:0)

你的意思是unbind,因为detach是其他东西,它会从DOM中删除元素,解除事件unbind

$("#btn1, #btn2").unbind("touch");

更新

更改包含js文件的orrder,首先必须加载jquery.js,然后再使用其他正在加载的库,其次我已从var删除了w并且onoff使它们成为全局范围,第三,我删除了onbodyload,而不是在ready处理程序中绑定触摸事件,如下所示,试试

<head>

<script type="text/javascript" charset="utf-8" src="jq.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
<script type="text/javascript" src="jquery.stopwatch.js"></script>

<script>
    w = new Stopwatch(updatedisplay, 50);
    onoff = false;

    function begin() {
        onoff = true;
        w.start();
    }

    function end() {
        w.stop();
        onoff = false;
    }       

    function updatedisplay(watch) {
        var mills = parseInt(watch.getElapsed().milliseconds/10);
        if(mills<10) {
            millis = '0'+mills;
        } else {
            millis = mills;
        }
        document.getElementById('counterValue').innerHTML = watch.toString() + "." + millis;
    }

    function reset() {
        w.stop();
        onoff = false;
        document.getElementById('counterValue').innerHTML = "00:00:00.00";
    }

$(document).ready(function(){
   $("#button-left, #button-right").bind("touch",begin);
});
</script>
</head>

<body>
  <div id="counterValue">
    00:00:00.00
  </div>
  <div id="button-left"></div>
  <div id="button-right"></div> 
</body>

答案 1 :(得分:0)

如上所述,我找到了一种方法来做我需要的...一点“阅读手册”,我想出了这个

$(document).ready(function() {                          
   document.getElementById('button-left').addEventListener('touchstart', nofunc(e), false);
   document.getElementById('button-right').addEventListener('touchstart', nofunc(e), false);
});

function vala(val) {
    av = val;
    checkvals();
}
function valb(val) {
    bv = val;
    checkvals();
}
function checkvals() {
    if(av == 1 && bv == 1) {
        if(onoff == false) {
            begin();
        } else {
            end();
        }
    }
}