Javascript – event.pageX/Y not working on touchmove

By | October 14, 2023

Today I had the following thing going on: I had an existing mousemove event and added touchmove later on, like this:

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

Unfortunately both variables were undefined on mobile devices.

After a while I fixed it. There’s a different event for touches. You can solve it like this:

$(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

I hope this will help others.

Category: Uncategorized