Dou*_*nks 10 javascript mobile user-interface touch
Prelim警告:我是js的新手,主要是通过网络搜索获得示例/教程.
我正在编写js,它应该在网络和移动设备(例如iPad)上运行.
我们有一个图书馆来帮助抽象出mouse
和之间的差异touch
:
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
};
}
Run Code Online (Sandbox Code Playgroud)
然后在代码中你可以做以下事情:
widget.view.bind(FLAGS.start, function(e) {
Run Code Online (Sandbox Code Playgroud)
我试图找到一个touch
等价物,mouseleave
所以我可以做一个类似的技巧.
我可以想象leave
通过跟踪位置move
并将其与有问题的小部件的边界框进行比较来捕获事件的方法,但我希望有一些像touchstart
/ mousedown
关系这样的单线程.
Ale*_*dam 14
有人建议,但未实施AFAIK: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");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14871 次 |
最近记录: |