here is my problem... Can you help me please?
$(".button").hover(function(){
$('.class').css({opacity: 1.0, visibility: "hidden"}).animate({opacity: 0}, 1200);
},function(){
$('.class').css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1}, 1200);
});
It only animates when it is appearing. :-(
Visibility is an animatable property according to the spec, but transitions on visibility do not work gradually, as one might expect. Instead transitions on visibility delay hiding an element.
Appearance of a CSS Transition on VisibilityWhile the transition is running, the element is visible regardless if it is a transition from visible to hidden or vice versa from hidden to visible (if using the default ease or the linear timing function).
visibility:hidden hides the element, but it still takes up space in the layout. display:none removes the element from the document. It does not take up any space.
The CSS animation-delay property has the following syntax: animation-delay: [time] | initial | inherit; As you can see, there are three possible values: time, initial, and inherit. The first option is [time], which is the number of seconds or milliseconds before the animation starts.
The visibility property in CSS is Boolean, either on or off.
In order to any animation to work, whether it's done with keyframes, transitions or jquery, the start point and end point of the property value set need to broken down into a set of steps, with a greater number of steps resulting in a smoother animation.
For a simple effect like this, a transition is best, please see the fiddle here. Use javascript just to add / remove classes that trigger the transition
.hidden {
opacity: 0;
transition: opacity 0.5s ease-in;
}
.show-me {
opacity: 1;
}
You set the transition property defining the property to transition, then the length, the ease function to use (which describes changes to the rate at which the animation has effect). You also need to define the start point and end point for your animated property as you can see with the two opacity values.
For the record - a keyframe is appropriate if your effect was more complex, like wanting one property to have fully animated half way through and then to animate back while another to take the full time, or for oscillations; and JQuery animate provides an easy way to act on the conclusion of an animation which is also sometimes necessary.
Try this way:
$(".button").hover(function(){
$('.class').css("opacity", "1.0").animate({opacity: 0}, 1200, function(){
$('.class').css("visibility", "hidden");
});
},function(){
$('.class').css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1}, 1200);
});
However, this is not the best option to fadeIn
and fadeOut
. You can use instead the methods with those names that jQuery provide, or better, use CSS transitions in case you can.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With