简体   繁体   English

在CSS中不显示任何内容,淡出不起作用或隐藏div

[英]Display none in CSS fade out does not work or hide the div

I have the following CSS transition - 我有以下CSS过渡-

 fadeinout-animate { width:200px; height: 200px; background: red; -webkit-animation: fadeinout 4s linear forwards; animation: fadeinout 4s linear forwards; opacity: 0; } @-webkit-keyframes fadeinout { 50% { opacity: 1; } } @keyframes fadeinout { 50% { opacity: 1; } } @keyframes fadeinout { 100% { opacity: 0;display:none; } } 

But, when I add this class to a div in Angular template, display none works but a blank space of 200*200 stays as is. 但是,当我将此类添加到Angular模板中的div时,显示不起作用,但保留200 * 200的空白。 (If I inspect element I still see that the red background alone is gone but manually applying display none inline style in browser hides or removes div) If i make height:0 that div goes off but it impacts behavior. (如果我检查元素,我仍然会看到仅红色背景消失了,但是在浏览器中手动应用显示无内联样式会隐藏或删除div)如果我将height:0设置为div,但会影响行为。 How do I fix it? 我如何解决它?

CSS can't animate between display: none ; CSS无法在显示之间设置动画:none ; and display: block ;. 显示:block ;。 Worse yet: it can't animate between height: 0 and height: auto. 更糟糕的是:它无法在height:0和height:auto之间设置动画。 So you need to hard code the height. 因此,您需要对高度进行硬编码。

Here's an attempt with CSS: 这是使用CSS的尝试:

Expected: the border should collapse but will it? 预期:边界应该崩溃,但是会崩溃吗?

 #a { width: 200px; height: 200px; background: red; -webkit-animation: fadeinout 4s linear forwards; animation: fadeinout 4s linear forwards; opacity: 0; } @keyframes fadeinout { 50% { opacity: 1; } 100% { opacity: 0; display: none; } } 
 <div style="border:1px solid black;width:200px;"> <div id="a"> </div> </div> 

Output: No, it won't. 输出:不,不会。

Now, lets try this.. 现在,让我们尝试一下。

 #a { width: 200px; height: 200px; background: red; -webkit-animation: fadeinout 4s linear forwards; animation: fadeinout 4s linear forwards; opacity: 0; } @keyframes fadeinout { 50% { opacity: 1; } 99% { opacity: 0; height: 200px; } 100% { height: 0px; } } 
 <div style="border:1px solid black;width:200px;"> <div id="a"> </div> </div> 

So, we basically collapsed the div after its transition was over by reducing its height to 0 and thus the parent container tag collapsed. 因此,在div过渡结束后,我们通过将其高度减小到0来基本上折叠了div,因此父容器标签也折叠了。

To add to @Highdef answer,. 要添加到@Highdef答案,。

If you don't mind adding a bit of Javascript, you can listen for the animation end event, and set display none manually. 如果您不介意添加一些Javascript,则可以侦听动画结束事件,并手动设置不显示。

 const e = document.getElementById("a"); e.addEventListener("animationend", (ev) => { if (ev.type === "animationend") { e.style.display = "none"; } }, false); 
 #a { width: 200px; height: 200px; background: red; -webkit-animation: fadeinout 4s linear forwards; animation: fadeinout 4s linear forwards; opacity: 0; } @keyframes fadeinout { 50% { opacity: 1; } 100% { opacity: 0; } } 
 <div style="border:1px solid black;width:200px;"> <div id="a"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM