I’m using this code to change opacity when user is on and off a picture unfortunately when the user clicks the image the opacity does not stay at 1. Anyone has an answer ?
$(document).ready(function(){
$('img#slide').animate({"opacity" : .7})
$('img#slide').hover(function(){
$(this).stop().animate({"opacity" : 1})
}, function(){
$(this).stop().animate({"opacity" : .7})
});
$('img#slide').click(function(){
$(this).animate({"opacity" : 1});
});
});
You need to somehow disable the
mouseleaveanimation when the user clicks.A common approach is to add a class, and have the
mouseleavecheck for the existence of the class.Test the live example: http://jsfiddle.net/KnCmR/
EDIT:
If you want a second click to revert the behavior back to the original, use
toggleClass()instead ofaddClass():jQuery docs:
.hasClass()– http://api.jquery.com/hasClass.addClass()– http://api.jquery.com/addClass.toggleClass()– http://api.jquery.com/toggleClass