Make the .hover () method more explicit and combine it with .on ():
var $close1 = $('#close_1'), $close2 = $('#close_2'); $('#close').on({ mouseenter: function(){ $close2.css({display:'none'}); $close1.css({display:'block'}); }, mouseleave: function(){ $close1.css({display:'none'}); $close2.css({display:'block'}); } });
Then combine this with .off ().
$('#close').on('touchstart',function(){ $(this).off('mouseenter,mouseleave'); });
If you want the event to fire when you click on touch devices, but when hovering on desktop devices, then place the functions as a separate function that you call in these actions accordingly.
EDIT
Some time has passed since I made this answer, here is the best way:
$(function(){ var isTouchDevice = ('ontouchstart' in window || 'onmsgesturechange' in window), $close = $('#close'), $close1 = $('#close_1'), $close2 = $('#close_2'); if(!isTouchDevice){ $close.on({ mouseenter: function(){ $close2.hide(); $close1.show(); }, mouseleave: function(){ $close1.hide(); $close2.show(); } }); } $close.on('click',function(){ $('#full_image').animate({height:0},300,function(){ $(this).find('img').attr('src','#'); }); $close.hide(); $close1.hide(); $close2.hide(); }); });
This does not require every event to fire with a βfreezeβ warning, it basically sets up page loading capabilities without affecting the click event.
PlantTheIdea
source share