我正在制作一个 Javascript 网络应用程序,但我一辈子都无法touchstart
触发该事件。我得到touchmove
和touchend
事件没问题。这是一个问题,因为我认为区分点击和滚动动作的最佳方法是将touchstart
事件的计数器归零,将其更新为touchmove
,然后将其比较为touchend
。我这样做是为了在点击结束时做一些动作,但不能滚动。例如,如果在您完成向下滚动列表后打开列表中的项目的页面会非常混乱,但能够点击项目以打开其页面会很好。
这就是我所拥有的:
// FIXME: this doesn't seem to ever fire
el.addEventListener('touchstart', function(e) {
// make sure that at the start of every touch we're not considered to be moving
alert("Touch starting");
app.__touchMoving = 0;
}, false);
el.addEventListener('touchmove', function(e) {
app.__touchMoving++;
}, false);
el.addEventListener('touchend', function(e) {
alert("Touch ended. We moved beforehand this many times: " + app.__touchMoving);
// if we are moving
if (app.__touchMoving > 0) {
// stop, since we're dragging, not tapping
return false;
}
// else we're no longer moving, so it was a tap
}
我从来没有看到touchstart
警报。如果我滚动touchend
将触发并且 app__touchMoving 将具有某种体面的价值。在旁注中,我注意到有时touchend
会似乎多次触发。
我在这里缺少一些基本的东西吗?很多人说这在 Android(和 iPhone)上应该可以正常工作,但第一个监听器似乎永远不会触发。
更新:我应该提到我一直在运行 Android 2.1 的三星 Galaxy S 上进行测试。