最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect touchmove lengthoffsets? - Stack Overflow

programmeradmin1浏览0评论

In developer tools I can see that touchmove event is fired multiple times while performing finger move*.

I'm looking for solution that should move div on which touchmove is performed - for exact number of touch move pixels, for either one or both axis.

What have I done so far?

I've tried to bind anonymous function to div's touchmove event, expecting that e argument of anonymous function will have something like offsetX & offsetY like mousemove do, so I could update position parameters of div accordingly.

Something like this:

$('div').on('touchmove', function(e){
  console.log(e);
  //e has no offset or similar param known to me
});

No results.

What should I do instead?

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.

* I'm actually doing cursor touch emulation in Chrome dev tools

In developer tools I can see that touchmove event is fired multiple times while performing finger move*.

I'm looking for solution that should move div on which touchmove is performed - for exact number of touch move pixels, for either one or both axis.

What have I done so far?

I've tried to bind anonymous function to div's touchmove event, expecting that e argument of anonymous function will have something like offsetX & offsetY like mousemove do, so I could update position parameters of div accordingly.

Something like this:

$('div').on('touchmove', function(e){
  console.log(e);
  //e has no offset or similar param known to me
});

No results.

What should I do instead?

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.

* I'm actually doing cursor touch emulation in Chrome dev tools

Share Improve this question asked Nov 5, 2015 at 15:45 Miloš ĐakonovićMiloš Đakonović 3,8715 gold badges38 silver badges57 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I would remend looking at this article that demonstrates a simple setup for the "touchmove" event and obtaining the offset position in jQuery: http://www.devinrolsen./basic-jquery-touchmove-event-setup/

In case the article disappears, here is the code setup with the offset calculated from the event from the article.

$('#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;
});

Hopefully this can help find a solution in plain javascript as well.

Here is a non jQuery solution.

function addTouchOffsets (event) {
    var touch = event.touches[0] || event.changedTouches[0];
    var realTarget = document.elementFromPoint(touch.clientX, touch.clientY);
    event.offsetX = touch.clientX-realTarget.getBoundingClientRect().x;
    event.offsetY = touch.clientY-realTarget.getBoundingClientRect().y
    return event;
}
发布评论

评论列表(0)

  1. 暂无评论