Created
March 29, 2014 08:01
-
-
Save dannyconnolly/9850447 to your computer and use it in GitHub Desktop.
Detecting touch events with js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(window){ | |
// check for touch | |
if (Modernizr.touch) { | |
// run the forEach on each figure element | |
[].slice.call(document.querySelectorAll("figure")).forEach(function(el,i){ | |
// check if the user moves a finger | |
var fingerMove = false; | |
el.addEventListener("touchmove",function(e){ | |
e.stopPropagation(); | |
fingerMove = true; | |
}); | |
// always reset fingerMove to false on touch start | |
el.addEventListener("touchstart",function(e){ | |
e.stopPropagation(); | |
fingerMove = false; | |
}); | |
// add hover class if figure touchend and fingerMove is false | |
el.addEventListener("touchend",function(e){ | |
e.stopPropagation(); | |
if (fingerMove == false) { | |
classie.toggle(el,"hover"); | |
} | |
}); | |
}); | |
} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment