Skip to content

Instantly share code, notes, and snippets.

@herrernst
Created September 6, 2017 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herrernst/131d1895e369ea6569bb954bfec0cefe to your computer and use it in GitHub Desktop.
Save herrernst/131d1895e369ea6569bb954bfec0cefe to your computer and use it in GitHub Desktop.
safari touchmove event preventDefault
<!doctype html>
<meta name="viewport" content="initial-scale=1">
<h1>safari touchmove event preventDefault</h1>
<div style="width: 300px; height: 300px; background: green"></div>
<h2>works</h2>
<pre>
document.addEventListener("touchmove", (e) => e.preventDefault())
</pre>
<h2>works</h2>
<pre>
document.addEventListener("touchstart", (e) => {
console.log("touchstart")
})
document.addEventListener("touchmove", (e) => {
e.preventDefault();
console.log("touchmove, should prevent")
})
</pre>
<h2>doesn't work</h2>
<pre>
document.addEventListener("touchstart", (e) => {
console.log("touchstart")
document.addEventListener("touchmove", (e) => {
e.preventDefault();
console.log("touchmove, should prevent")
})
})
</pre>
<h2>doesn't work</h2>
<pre>
document.addEventListener("touchstart", (e) => {
console.log("touchstart")
})
setTimeout(() => {
document.addEventListener("touchmove", (e) => {
e.preventDefault();
console.log("touchmove, should prevent")
})
}, 0)
</pre>
<script>
//copypaste cases from above to test here
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment