Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google recommends using what they call "passive listeners" for touch handlers on mobile devices #19

Open
loupiote opened this issue Jan 31, 2023 · 1 comment

Comments

@loupiote
Copy link

loupiote commented Jan 31, 2023

Google recommends using what they call "passive listeners" for touch handlers on mobile devices, to improve scrolling performance.

https://developer.chrome.com/en/docs/lighthouse/best-practices/uses-passive-event-listeners/

So I think the plugin code should be changed to use this:

document.addEventListener('touchstart', onTouchStart, {passive: true});
etc...

The vast majority of mobile devices used today support this, and those who want to support other devices could detect if the browser supports it if needed.

@loupiote
Copy link
Author

loupiote commented Feb 1, 2023

I modified the plugin to resolve the following issues:

  1. prevent spurious swipe events when double-tap is detected. This prevent spurious swipe events when people use the one-finger "double-tap + drag" zoom feature on Firefox.

  2. prevent spurious swipe events when multi-finger gestures are detected. This prevents spurious swipe events when people do zoom-in / zoom-out gestures that could otherwise trigger spurious swipe events.

  3. prevent spurious swipe when people scroll the window by a motion that is not completely vertical (or horizontal for horizontally scrolling window), useful for Chrome.

  4. added an optional trace/log function (quite useful for remote debugging)

  5. added config flags to support passive handler, returned value etc.

  6. fixed a bug: swipeleft/swiperight should occur if abs(dx) >= threshold AND abs(dx) > abs(dy) -- the second part of the condition was missing from the code, sometimes causing swipeleft/swiperight in case where swipeup/swipedown should have occurred.

All this have been tested on Chrome, Firefox, Opera and Safari mobile browsers.

I still have a problem: On Firefox mobile (android), touch events are not sent to the handler if "some" scrolling happens when you swipe, even if you swipe mostly right/left. No such problem with Chrome (on Chrome, events are always sent to the handler, whether they cause a scroll or not - we just need to prevent spurious events when a scroll occurred, which we have implemented).

For FF. It is possible to disable default handling, but in that case scrolling does not happen at all, which is an issue in sites that require vertical scroll support and swipe right/left at the same time (like my site). It may be possible to disable default handling and still send the events for the default handling AFTER they have been processed by the plugin. I have not yet managed to do that... Maybe something like what is described there: https://stackoverflow.com/questions/7610871/how-to-trigger-an-event-after-using-event-preventdefault

I can provide you my code, if you are interested to update your plugin. I feel it is significant improvement for usability, especially with the prevention of spurious events in case of double tab, multi-finger gesture, and gesture causing scroll (on Chrome).

It is the one I currently on my photo website with swiping photos in an album. let me know of the best way to provide the updated code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant