question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why disable touch events on inline gallery?

See original GitHub issue

I created an inline gallery instance following instructions in the FAQ and quickly discovered the limitation of vertical scrolling on touch-enabled devices. The page won’t scroll if the touch gesture originates over the inline gallery.

Reading the source, it looks like vertical scroll is prevented by a number of factors:

  • CSS: touch-events: none on .pswp and .pswp__container;
  • JS: e.preventDefault() in _onDragMove() handler;
  • JS: e.preventDefault() in _onDragStart() handler as a result of _preventDefaultEventBehaviour() call (which can be overridden with preventObj.prevent = false in the preventDragEvent event handler, as described in the API docs).

I created a fork of PhotoSwipe that fixes the scroll issue for my use case and allows touch events to pass when _options.modal == false.

Before attempting to provide a pull request, can you briefly explain if this has been considered before? My proposed solution is when _options.modal == false:

  • CSS: enable touch events in for .pswp and .pswp__container;
  • JS: do not preventDefault() in _onDragStart() and _onDragMove()
  • JS: check _direction in _onDragMove() and if the value is "h" (horizontal), then preventDefault() to avoid vertical scrolling while swiping.

I saw a similar solution was proposed in #1050.

Can you explain why the current behaviour should be enforced even when the gallery is inline?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rafaelderolezcommented, Aug 8, 2016

@oslego Thanks a lot for the preventDragEvent handler. It solved my problem with touch devices as well.

1reaction
oslegocommented, Mar 11, 2016

@newsomc See the “non-modal-scroll” branch in my fork: https://github.com/oslego/PhotoSwipe/tree/non-modal-scroll

The diff should explain what I changed in src/css/main.scss, src/js/core.js and src/js/gestures.js. The other changes are just the result of the build.

This is the setup for my gallery, the “preventDragEvent” handler is necessary:

var options = {
  history: false,
  maxSpreadZoom: 1,
  index: 0,
  focus: false,
  escKey: false,
  modal: false,
  pinchToClose: false,
  closeOnScroll: false,
  closeOnVerticalDrag: false
}

// items is the Array of photo configs
var gallery = new PhotoSwipe($('.pswp'), false, items, options);
gallery.listen('preventDragEvent', function(e, isDown, preventObj) {
  preventObj.prevent = false;
});
gallery.init();

This solution isn’t bullet-proof, as @dimsemenov mentioned, but it worked for my use case. Maybe it helps you too. Swiping with the mouse on a laptop may cause positioning issues, but it worked well on a touch-enabled device.

Read more comments on GitHub >

github_iconTop Results From Across the Web

touch-action - CSS: Cascading Style Sheets - MDN Web Docs
Applications using Touch events disable the browser handling of gestures by calling preventDefault() , but should also use touch-action to ...
Read more >
ChromeDriver touch events always disabled
First there's whether or not the browser supports the touch event APIs. That's controlled by the -touch-events flag (enabled, disabled or automatic which...
Read more >
touch-action
Setting touch-action to none will disable all browser handling of these events, leaving them up to you to implement (via JavaScript).
Read more >
HTML5 Canvas Mobile Touch Events Tutorial
Do you mean something like "stage.draggable(false)"? It is not working. It works if I disable listening, but my stage needs events, so I...
Read more >
Touch events - Notes of Phaser 3 - GitHub Pages
This is an expensive process, should only be enabled on Game Objects that really need it. Disable interactive¶. Disable temporary. gameObject.disableInteractive ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found