0

I would like to prevent touchStart events in order to prevent Safari from bouncing in iOS devices under certain conditions.

To do so I'm using the folowing:

$('.wrapper').on('touchstart', function(e) {
    e.preventDefault();
});

But now none of the click events work on it:

$('.wrapper').on('click', function() {
    $('.wrapper').text('Click fired');
});

Reproduction online

I can not replace the click event for any other one. As this might come from the end user.

Previously to iOS 10, the bouncing could be avoided by preventing only touchMove but since iOS 10 it will bounce unless touchStart is prevented too.

Is there any way to prevent only the touchStart event but allow the use of the click event?

Community
  • 1
  • 1
Alvaro
  • 40,778
  • 30
  • 164
  • 336

1 Answers1

0

I don't have iOS 10 available for a test-drive, and this is a wild guess, but check this snippet:

$('.wrapper').on('click touchstart', function(e) {
  if (e.type != 'click') {
    e.preventDefault();
  } else {
    $('.wrapper').text('Click fired');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">asd</div>
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • Nop, not working. It seems to fire always the `touchStart` before the `click` one. Therefore it prevents the click. – Alvaro Oct 17 '16 at 20:40
  • How about changing the `click` to `mousedown`? – Dekel Oct 17 '16 at 20:47
  • As i said, that's not an option. Developers can use any event they want and will very probably use `click`. Found a work around. But looks more like a hack :D I trigger the click event on the event target after preventing the default: https://jsfiddle.net/8n47whd0/5/ – Alvaro Oct 17 '16 at 20:56
  • @Dekei not sure if that would solve the problem. But not interested in adding such a "big" script anyway. – Alvaro Oct 17 '16 at 21:02
  • Ok. It seems like a known issue, so this script might also fix some other problems you might haven't think about, yet. – Dekel Oct 17 '16 at 21:03
  • The purpose of the library is to prevent delays. Which might or might not be a possible solution to my problem. But still, it seems to me an overkill to add such a script to solve this specific problem. – Alvaro Oct 17 '16 at 21:24