Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate "visibility: hidden"?

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. :-(

like image 236
Ondřej Moldan Avatar asked Jun 07 '15 13:06

Ondřej Moldan


People also ask

Can visibility be animated?

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.

Does transition work with visibility?

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).

What is the difference between visibility hidden and display none?

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.

How do I delay an animation in CSS?

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.


2 Answers

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.

like image 74
Toni Leigh Avatar answered Oct 10 '22 22:10

Toni Leigh


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.

like image 3
lmgonzalves Avatar answered Oct 10 '22 23:10

lmgonzalves