0

I create a animation that works when I hover over it, but due to mobile reasons I want the animation to play when the user clicks or touches it.

Here is my js code that makes the hover work, but not sure how to make the onclick work.

$(document).ready(function() {     
    $('.phone').hover(function(){     
        $('.phone').addClass('phone-two');    
    },     
    function(){    
        $('.phone').removeClass('phone-two');     
    });
}); 

Here is a JSFiddle to explain it better.

daugaard47
  • 1,726
  • 5
  • 39
  • 74

3 Answers3

2

With $.on() you can add the 'touchstart' and 'touchend' event.

$(document).ready(function() {     
$('.phone').on({
    'mouseover touchstart': function(){     
         $('.phone').addClass('phone-two');    
     },     
     'mouseout touchend': function(){    
         $('.phone').removeClass('phone-two');     
    }});
});

note that this also preserves your hover event.

here is a great post on mobile touch events: How to recognize touch events using jQuery in Safari for iPad? Is it possible?

DEMO

Community
  • 1
  • 1
13ruce1337
  • 753
  • 1
  • 6
  • 13
1

To bind all of your events, use this:

$('.phone').on("mouseover click touchstart touchend",function(){
    $('.phone').toggleClass('phone-two');    
});

Reference: http://api.jquery.com/on/

scald
  • 729
  • 1
  • 7
  • 10
1

Try this

$('.phone').on('click', function (e) {  
    $('.phone').toggleClass('phone-two');  
});
benjamin54
  • 1,280
  • 4
  • 28
  • 48