i want a certain div to be shown in certain circumstances, and i want it to be animated, at the start it should be invisible so...
.fixedNav{
background-color: rgba(28,20,13, 0.75);
position:fixed;
color:white;
width:60vw;
margin-top:-50px !important;
z-index: 1;
display:none;
opacity:0;
transition:opacity .3s ease-in;
}
and then i animate the opacity with css by adding this class, with jquery, to it:
.fixedNavActive{
display: block;
opacity:1;
}
and it all works fine if i remove the display: none; from the main class, if i leave it there then the opacity isn't animated, it just appears, why does it break it and how can i make it animate the opacity instead of toggling it to 1?
When we want to use transition for display:none to display:block, transition properties do not work. The reason for this is, display:none property is used for removing block and display:block property is used for displaying block. A block cannot be partly displayed.
You can't use the display property in CSS animations either.
display: none doesn't have a literal opposite like visibility:hidden does. The visibility property decides whether an element is visible or not. It therefore has two states ( visible and hidden ), which are opposite to each other.
$("div"). animate({ opacity:0 },"slow"); This is useful if you also want to animate other properties of the element at the same time.
Basically, you cannot animate display properties, and even though you're only trying to animate the opacity
, that transition fails because when the transition begins, the element doesn't exist. It has been removed from the flow due to the display: none
property.
You can achieve the effect you're looking for by using visibility: hidden
and visibility: visible
, which is an animatable property, and if you need to effectively remove the element from the flow when it lacks visibility, then you can apply a max-height
of 1px
to the element and a margin-top
of -1px
.
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