iOS/Android 浏览器:在 touchmove 上显示按钮的缩放预览
我正在尝试创建类似于 iPhone 键盘的行为(请参阅包含的图片)。例如,当用户开始并移动触摸时,她会看到所触摸元素的缩放版本,并且在触摸时会发生选择。我正在使用 Zepto。
我可以正确获取触摸坐标,但无法找到手指下方的物体。我使用下面的代码来检查哪个元素作为事件的目标返回。
$("#myList li").live('touchmove', function(event) {
console.log(event.touches[0].target.innerHTML);
});
这始终返回 starttouch 上的事件。
在 Apple 文档上应该有 事件.touches 事件.changedTouches event.targetTouches
我尝试检查每个对象上的第一个元素,但它们似乎都只包含 starttouch-element 作为目标。我在这里缺少什么吗?
我从如果其他方法都失败了,正确地获得了触摸的坐标
var positionTop = event.touches[0].pageY;
var positionLeft = event.touches[0].pageX;
,我开始思考也许有一种方法可以用坐标找到触摸下的 DOM 元素。
任何想法表示赞赏。
I'm trying to create behavior similar to iPhone keyboard (see included image). As in, when user starts and moves touch, she sees a zoomed version of the element that is touched, and selection would happen when on touch up. I'm using Zepto.
I can get the touch coordinates correctly, but have trouble finding the object that's under the finger. I'm using below code to check which element is returned as target of the event.
$("#myList li").live('touchmove', function(event) {
console.log(event.touches[0].target.innerHTML);
});
This returns always the event that was on starttouch.
On Apple documentation the touch should have
event.touches
event.changedTouches
event.targetTouches
I've tried checking the first element on each of the objects, but they all seem to contain just the starttouch-element as target. Is there something I'm missing here?
I get the coordinates for the touch correctly from
var positionTop = event.touches[0].pageY;
var positionLeft = event.touches[0].pageX;
If all else fails, I started to think maybe there's a way to find the DOM-element under the touch with the coordinates.
Any ideas are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
触摸事件下的对象由
event.target
给出。另外,您应该绑定到touchstart
和touchend
事件。请参阅我制作的示例:http://ampersand.no.de/iOSkeyboard.html
源代码:
更新
好的,我找到了它,“它”被称为
document.getElementFromPoint()
。 Mozilla 网站上有一些文档 (https://developer.mozilla.org/En/DOM/Document.elementFromPoint)。它将获取给定顶部和左侧坐标的元素。这可以很容易地用来跟踪手指当前位于哪个键上。我已经更新了我的代码示例以使用这个很棒的函数。以下是一些更新的函数,用于获取当前触摸元素并更新“悬停键”:查看示例 看看如何使用所有这些。我已经在 iPad 2 (iOS 4.3) 上对此进行了测试,并且有效。它仍然需要一些调整来提高平滑度并考虑触摸移动最终出现在非关键元素上的情况。我很想知道这在 Android 上是如何工作的。我还没有完成第三个键盘的文本输入代码,但是您可以结合我之前的一些代码来使其工作。祝你好运。
注意:阻止 touchmove 事件在 DOM 树上传播/冒泡是极其重要的。如果它确实传播,那么它将变成滚动,并且在 iOS 设备上滚动期间 DOM 操作被禁用,因此您将无法更新“悬停键”。当 touchmove 事件在非键元素上触发时,您需要考虑所有边缘情况。
The object that is under the touch event is given by
event.target
. Also, you should bind totouchstart
andtouchend
events. See this example I made:http://ampersand.no.de/iOSkeyboard.html
Source code:
UPDATE
Ok, I found it, and 'it' is called
document.getElementFromPoint()
. There is some documentation on Mozilla's site (https://developer.mozilla.org/En/DOM/Document.elementFromPoint). It'll get the element given the top and left coordinates. This can be easily used to track which key the finger is currently over. I've updated my code example to use this awesome function. Here are some updated functions that get the current touch element and update the 'hover key':Check out the example to see how all of this is used. I have tested this on iPad 2 (iOS 4.3), and it works. It still needs some tweaking to improve smoothness and account for cases when the touchmove ends up on a non-key element. I'd be interested to see how this works on Android. I haven't completed the text entry code for the third keyboard, but you can combine some of my previous code to make it work. Good luck.
Note: It is extremely important to block touchmove event propagation/bubbling up the DOM tree. If it does propagate, then it will turn into a scroll, and during scrolling on iOS devices DOM manipulation is disabled, so you won't be able to update the 'hover key'. You need to account for all edge cases when the touchmove event fires over a non-key element.