Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade in with CSS visibility: visible?

I'm using visibility: hidden; and visibility: visible; to show and hive divs, such as modals and other divs. I'd like to have a fade-in effect when clicking the <a> link to run the javascript to show and hide the divs, but I'd very strongly like to continue using the visibility element to toggle the div visibility. Is there a way to do this with HTML/CSS/JavaScript/jQuery?

like image 831
tkbx Avatar asked Nov 13 '12 15:11

tkbx


People also ask

Does transition work with visibility?

The transition element of vue only works with display:none but not visibility:hidden .

How do you add transitions to visibility?

Just as transitions on other properties you can define a transition on visibility, e.g. using " transition:visibility 1s". This example specifies a duration of 1 second. There are still just the two states visible and hidden (for normal non-table elements).

How do you make a fading effect in CSS?

In the CSS, use the @keyframes rule paired with fadeIn. At 0%, set the opacity to 0. At 100%, set the opacity to 1. This creates the fade-in effect.

Can visibility be animated in CSS?

css animates opacity and other CSS properties; you can't do a CSS transition on the display property. So an invisible object remains and it takes up space in the document flow. If you google a bit, you'll find some solutions that suggest using a timer to set display: none at the end of the animation.


2 Answers

in jQuery:

$('selector').fadeIn();

With CSS, use opacity: (this is 50%)

-moz-opacity:.50; 
filter:alpha(opacity=50); 
opacity:.50;

If you would like to do the fadeIn manually, adjust this opacity in steps, and when you reach the point of invisibilty, add display:none But if you use jQuery anyways, stick with fadeIn()

fadeIn() supports speed too, just add the milliseconds as first parameter. Look at the docs: http://api.jquery.com/fadeIn/

Want to keep the display property in css, use fadeTo(). It requires the opacity percentage: http://api.jquery.com/fadeto/

$(this).fadeTo("slow", 1); // 100% visible
$(this).fadeTo("slow", 0); // 0% visible aka hidden
like image 138
Rene Pot Avatar answered Nov 09 '22 05:11

Rene Pot


I assume you want to use visibility:hidden instead of display:none so that you see an empty space.

If so fadeIn() and fadeOut() won't work for you because it sets the display to none after it fades out.

Instead use jQuery animate() to animate the opacity and then add visibility:hidden/visible in the callback.

Here is how I would do it with jQuery.

$('#a-id').click(
  if( $('#div-id').css('visibility') == 'hidden'){
    $('#div-id').animate({opacity: 1}, 'fast', function(){
    $('#div-id').css('visibility', 'visible');
  });
  }else{
    $('#div-id').animate({opacity: 0}, 'fast', function(){
    $('#div-id').css('visibility', 'hidden');
  }
);

I'm sure there is a better way of doing it but this works without problems afaik.

like image 25
Hrvoje Miljak Avatar answered Nov 09 '22 06:11

Hrvoje Miljak