2011-04-21 67 views
7

预先警告:我对js很陌生,主要通过搜索网络搜索示例/教程来获得。javascript等效于'mouseleave'的触摸互动

我写的js应该可以在网络和移动设备上运行(例如iPad)。

我们有一个库,帮助抽象掉mousetouch之间的差异:

if (navigator.userAgent.search('Mobile') > 0) { 
    window.FLAGS = { 
    start: 'touchstart', 
    end: 'touchend', 
    move: 'touchmove', 
    click: 'click', 
    touchScreen: true 
    }; 
} else { 
    window.FLAGS = { 
    start: 'mousedown', 
    end: 'mouseup', 
    move: 'mousemove', 
    click: 'click', 
    touchScreen: false 
    }; 
} 

然后在代码中,你可以做这样的事情:

widget.view.bind(FLAGS.start, function(e) { 

我试图找到相当于一个touchmouseleave,所以我可以做一个类似的伎俩。

我能想象的方式通过跟踪move位置和比较,为边界问题widget的盒子搭上leave事件,但我希望有一个小的一行像touchstart/mousedown关系。

回答

7

它已经提出,但据我所知没有实现:http://www.quirksmode.org/mobile/advisoryTouch.html

像这样的东西可能会奏效(从我的头顶,未经测试写它):

var element; 
document.addEventListener('touchstart', function(event) { 
    event.preventDefault(); 
    var touch = event.touches[0]; 
    element = document.elementFromPoint(touch.pageX,touch.pageY); 
}, false); 

document.addEventListener('touchmove', function(event) { 
    event.preventDefault(); 
    var touch = event.touches[0]; 
    if (element !== document.elementFromPoint(touch.pageX,touch.pageY)) { 
     touchleave(); 
    } 
}, false); 

function touchleave() { 
    console.log ("You're not touching the element anymore"); 
} 
+0

谢谢。我认为关于“未实现”的一点是我需要听到的,我倍加赞赏解决方法的示例代码。 – 2011-04-21 21:46:21

+1

你的帖子已经过时了,但是 - 不应该是touchmove事件的“更换手机”吗?至少在iPad上触摸数组是undefined onTouchMove - 比较http://stackoverflow.com/a/7236327/818732 – 2012-07-14 11:51:01