I am making a mobile website that uses JavaScript touch events. Things work fine in iOS Safari and Chrome for Android, but the stock Android Browser (version 4.1.2) is giving me trouble.
During a touch process, the touchstart
and touchmove
events are called as expected. However, one of the actions performed by the touchmove
handler seems to trigger a premature touchcancel
event. (I'm not sure whether this is significant, but the action that triggers the touchcancel
is the modification of an SVG object's viewBox
attribute.) If I comment out this action, the touch process proceeds normally (i.e., completion of touchmove
through to touchend
).
All of my touch handlers call the preventDefault()
function, so the issue isn't the one that's described in this bug: https://code.google.com/p/android/issues/detail?id=19827.
I've read that there is a lot of inconsistency among browsers as to when touchcancel
is called. The stock Android browser is the only one that is problematic for me.
Is there a workaround out there? For example, is there away I can completely disable the touchcancel
event? Another idea I had was to have the touchcancel
handler programmatically trigger another touchstart
/touchmove
event, but I didn't get very far with that. Any ideas would be appreciated.
When using Pointer events, touch-action: pan-y will allow the browser to perform regular vertical scrolling, and won't fire pointercancel (the touchcancel equivalent) when the gesture is performed vertically.
Definition and Usage. The touchcancel event occurs when the touch event gets interrupted. Different devices will interrupt a touch event at different actions, and it is considered good practice to include this event to clean up code if this "error" should occur.
I know it's a bit late but if someonee else is facing this issue, here's a small jQuery extension that explains how to deal with pointercancel
and touchcancel
events.
Basically, if you switch to pointer events you will be able to prevent pointercancel
by simply using the touch-action
CSS property with a value of none
, as long as the browser has both features correctly implemented. (Android 5+, Chrome 55+, IE11, Edge, etc)
If you really must use the legacy touch events instead, you'll have to implement event.preventDefault()
in your touchmove events and this will prevent touchcancel
from firing, but it will also disable entirely the browser handling of any default action like a pan or click.
As a final note, I wouldn't use touch events + touch-action CSS rules because touch-action
was only recently added, at the same that that Pointer Events. So while that combination may work in newer browsers, it will most certainly fail in older ones (by triggering touchcancel
event unexpectedly).
Check the README from the jQuery extension I posted because it explains the implications of using either TouchEvent and PointerEvent interfaces.
If you are useing hammer.js, you can disable pointer-events (which cause problems like this), by adding:
delete window.PointerEvent;
in your index.html PRIOR to loading hammer.js, and it will ignore those events.
You can also set SUPPORT_POINTER_EVENTS = false; (line 384 of v2.0.8) to do the same thing.
Ideally the devs would add the ability to turn this off, but so goes the open source dilemma...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With