I have some JavaScript to handle touch events on a web page and watch for gestures that are predominantly horizontal. When the user begins to swipe horizontally, I want the effects of their gesture to begin, but when they swipe vertically, the page should scroll naturally as it would otherwise.
While horizontal gestures do get the desired effect right now, they also still seem to cause vertical scroll for the amount the touch moves up or down, even while the majority of the movement is horizontal. How would I fix this to prevent vertical scrolling when swiping predominantly horizontally? I've used both preventDefault()
and stopPropagation()
to no effect. I imagine there's something really obvious I'm missing, but I just can't seem to find it.
I've tried my best to search for similar questions, but my search queries are yet to return anything that works.
Note: I don't want to stop vertical scrolling altogether, and even if there's an X-value change in the gesture, the page should still scroll if the Y-change is greater. I only want the vertical scroll to lock if the X-change is greater than the Y-change (which is what my code should do right now).
Update: I hadn't thought to retrieve console logging from a mobile device. On @tushar-shukla's suggestion, I checked it, discovering the following error:
[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See <URL>
Here is a watered-down version of my code (and a JSFiddle, if that's more your cup of tea):
var startX = undefined;
var startY = undefined;
function touchmove(ev) {
var e = ev.originalEvent;
// Set the initial start position if not already. Could also be done on touchstart
if (startX===undefined) { startX=e.touches[0].pageX; }
if (startY===undefined) { startY=e.touches[0].pageY; }
// Calculate the change or displacement from the start.
changeX = e.touches[0].pageX-startX;
changeY = e.touches[0].pageY-startY;
// If the change horizontally is greater than the change vertically...
if (Math.abs(changeX)>Math.abs(changeY)) {
// Do something with the gesture
// Prevent vertical scrolling; Apparently this isn't doing it:
ev.preventDefault();
ev.stopPropagation();
e.preventDefault();
e.stopPropagation();
}
}
$(document).on("touchmove",touchmove);