在开发人员工具中,我可以看到在执行手指移动时多次触发 touchmove
事件*。
我正在寻找应该移动执行 touchmove 的 div
的解决方案 - 对于触摸移动像素的确切数量,对于一个或两个轴。
到目前为止我做了什么?
我试图将匿名函数绑定(bind)到 div
的 touchmove
事件,期望匿名函数的 e
参数有类似offsetX
和 offsetY
就像 mousemove 一样,所以我可以相应地更新 div
的位置参数。
像这样:
$('div').on('touchmove', function(e){
console.log(e);
//e has no offset or similar param known to me
});
没有结果。
我应该怎么做?
Special appeal: I will upvote any working solution, but accepted answer goes to the one that will give me at least a starting point about what should I do in plain JavaScript, without abstractions like jQuery mobile.
* 我实际上是在 Chrome 开发工具中进行光标触摸模拟
最佳答案
我建议您查看这篇文章,它演示了“touchmove”事件的简单设置并在 jQuery 中获取偏移位置:http://www.devinrolsen.com/basic-jquery-touchmove-event-setup/
以防文章消失,这里是代码设置,其中根据文章的事件计算偏移量。
$('#someElm').bind('touchmove',function(e){
e.preventDefault();
var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
var elm = $(this).offset();
var x = touch.pageX - elm.left;
var y = touch.pageY - elm.top;
});
希望这也有助于在普通 javascript 中找到解决方案。
关于javascript - 如何检测触摸移动长度/偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33548926/