Posted By

jbernus on 12/10/13


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

FlashBuddy
bcmoney
fhenderson


Switch hover/touch events for Desktop/mobile devices


 / Published in: JavaScript
 

Hover events can be irrelevant on mobile devices relying on touch events. This script switches the classical desktop hover events to touch events.

Inspired by: http://jbkflex.wordpress.com/2012/07/12/javascript-touch-event-or-mouse-event-detect-and-handle-according-to-device/

  1. var isTouchSupported = 'ontouchstart' in window;
  2. var startEvent = isTouchSupported ? 'touchstart' : 'mouseenter';
  3. var endEvent = isTouchSupported ? 'touchend' : 'mouseleave';
  4.  
  5. $('.yourclass').on(startEvent,function(){
  6. doSomething(this);
  7. });
  8.  
  9. $('.yourclass').on(endEvent,function(){
  10. doSomething(this);
  11. });

Report this snippet  

You need to login to post a comment.