event.pageX/Y在touchmove事件中无法工作

7

今天我遇到了以下情况:我有一个现有的 mousemove 事件,后来添加了 touchmove,就像这样:

$(window).on "mousemove touchmove", (e) ->
  pos_x = e.pageX
  pos_y = e.pageY

不幸的是,在移动设备上这两个变量都是undefined


1
e.originalEvent.touches[0].pageX - Wasim A.
1个回答

24

过了一会儿,我修复了它。触摸事件有所不同。您可以像这样解决:

$(window).on "mousemove touchmove", (e) ->
  touch = undefined
  if e.originalEvent.touches
    touch = e.originalEvent.touches[0]
  pos_x = e.pageX or touch.pageX
  pos_y = e.pageY or touch.pageY

我希望这可以帮助其他人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接