Skip to content

Instantly share code, notes, and snippets.

@terrymun
Created August 21, 2017 07:23
Show Gist options
  • Save terrymun/967157a6a328ff17e873b425103dd733 to your computer and use it in GitHub Desktop.
Save terrymun/967157a6a328ff17e873b425103dd733 to your computer and use it in GitHub Desktop.
Stop touch events from bubbling up in iOS Safari
/*
* Stops touchevent from bubbling up using jQuery
*/
$(document).on('touchstart touchmove touchend', function(e) {
// Example of a parentSelector
// var parentSelector = '#parentElement';
if ($(e.target).closest(parentSelector).length)
e.preventDefault();
});
/*
* Stops touchevent from bubbling up using JavaScript
*/
document.addEventListener('touchend', bubbleStop, false);
document.addEventListener('touchmove', bubbleStop, false);
document.addEventListener('touchend', bubbleStop, false);
var bubbleStop = function(e) {
// If you need polyfill for .closest(), see: https://stackoverflow.com/a/35294561/395910
// Example of a parentSelector
// var parentSelector = '#parentElement';
if(e.target.closest(parentSelector))
e.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment