Javascript 是否有与单击事件相同的 e.PageX 位置的“touchstart”事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4780837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:03:39  来源:igfitidea点击:

Is there an equivalent to e.PageX position for 'touchstart' event as there is for click event?

javascriptjqueryeventsclicktouch

提问by waxical

I'm trying to get the X position with jQuery of a touchstart event, used with the live function?

我正在尝试使用与 live 功能一起使用的 touchstart 事件的 jQuery 获取 X 位置?

I.e.

IE

$('#box').live('touchstart', function(e) { var xPos = e.PageX; } );

Now, this does work with 'click' as the event. How on earth (without using the alphajQuery Mobile) do I get it with a touch event?

现在,这确实适用于 'click' 作为事件。我到底如何(不使用alphajQuery Mobile)通过触摸事件获得它?

Any ideas?

有任何想法吗?

Thanks for any help.

谢谢你的帮助。

回答by mkoistinen

Kinda late, but you need to access the original event, not the jQuery massaged one. Also, since these are multi-touch events, other changes need to be made:

有点晚了,但您需要访问原始事件,而不是 jQuery 按摩事件。此外,由于这些是多点触控事件,因此需要进行其他更改:

$('#box').live('touchstart', function(e) {
  var xPos = e.originalEvent.touches[0].pageX;
});

If you want other fingers, you can find them in other indices of the touches list.

如果您需要其他手指,您可以在触摸列表的其他索引中找到它们。

UPDATE FOR NEWER JQUERY:

更新较新的 JQUERY:

$(document).on('touchstart', '#box', function(e) {
  var xPos = e.originalEvent.touches[0].pageX;
});

回答by Nedudi

I use this simple function for JQuery based project

我将这个简单的函数用于基于 JQuery 的项目

    var pointerEventToXY = function(e){
      var out = {x:0, y:0};
      if(e.type == 'touchstart' || e.type == 'touchmove' || e.type == 'touchend' || e.type == 'touchcancel'){
        var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
        out.x = touch.pageX;
        out.y = touch.pageY;
      } else if (e.type == 'mousedown' || e.type == 'mouseup' || e.type == 'mousemove' || e.type == 'mouseover'|| e.type=='mouseout' || e.type=='mouseenter' || e.type=='mouseleave') {
        out.x = e.pageX;
        out.y = e.pageY;
      }
      return out;
    };

example:

例子:

$('a').on('mousedown touchstart', function(e){
  console.log(pointerEventToXY(e)); // will return obj ..kind of {x:20,y:40}
})

hope this will be usefull for you ;)

希望这对你有用;)

回答by matt.chatterley

I tried some of the other answers here, but originalEvent was also undefined. Upon inspection, found a TouchList classed property (as suggested by another poster) and managed to get to pageX/Y this way:

我在这里尝试了其他一些答案,但 originalEvent 也未定义。经过检查,发现了一个 TouchList 类属性(如另一张海报所示)并设法通过这种方式访问​​ pageX/Y:

var x = e.changedTouches[0].pageX;

回答by Scott Jungwirth

If you're not using jQuery... you need to access one of the event's TouchLists to get a Touchobject which has pageX/YclientX/Yetc.

如果您不使用 jQuery ...您需要访问事件之一TouchList以获取Touch具有pageX/YclientX/Y等的对象。

Here are links to the relevant docs:

以下是相关文档的链接:

I'm using e.targetTouches[0].pageXin my case.

e.targetTouches[0].pageX在我的情况下使用。

回答by Tom Tu

Check Safari developer reference on Touch class.

在 Touch 类上查看 Safari开发人员参考

According to this, pageX/Y should be available - maybe you should check spelling? make sure it's pageXand not PageX

据此, pageX/Y 应该可用 - 也许你应该检查拼写?确保它是pageX而不是PageX