只有在动画之后才能使用CSS隐藏/移除div所带的空格(Hide / remove space taken by div from html with css only after animation

编程入门 行业动态 更新时间:2024-10-26 08:22:43
只有在动画之后才能使用CSS隐藏/移除div所带的空格(Hide / remove space taken by div from html with css only after animation)

我试图“物理”删除,或让div(例如)在隐藏后不占用屏幕上的空间。

我在#test div下面添加了另一个div,以显示在#test被隐藏后,它仍占用屏幕上的空间。 有没有办法让这个空间消失,只有CSS?

#test {
  opacity: 0;
  -webkit-animation: fadeinout 3s;
  /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadeinout 3s;
  /* Firefox < 16 */
  -ms-animation: fadeinout 3s;
  /* Internet Explorer */
  -o-animation: fadeinout 3s;
  /* Opera < 12.1 */
  animation: fadeinout 3s;
}

@keyframes fadeinout {
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@-moz-keyframes fadeinout {
  /* Firefox */
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@-webkit-keyframes fadeinout {
  /* Safari and Chrome */
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@-o-keyframes fadeinout {
  /* Opera */
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
} 
  
<div id="test">hide me after 3 seconds</div>
<div>After 3 seconds I don't move up.</div> 
  
 

I'm trying to "physically" remove, or have the div (for example) not take up space on the screen after hidden.

I added another div below the #test div to show that after #test is hidden, it still takes up space on the screen. Is there a way to have that space be gone, with css only?

#test {
  opacity: 0;
  -webkit-animation: fadeinout 3s;
  /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadeinout 3s;
  /* Firefox < 16 */
  -ms-animation: fadeinout 3s;
  /* Internet Explorer */
  -o-animation: fadeinout 3s;
  /* Opera < 12.1 */
  animation: fadeinout 3s;
}

@keyframes fadeinout {
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@-moz-keyframes fadeinout {
  /* Firefox */
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@-webkit-keyframes fadeinout {
  /* Safari and Chrome */
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

@-o-keyframes fadeinout {
  /* Opera */
  from {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
} 
  
<div id="test">hide me after 3 seconds</div>
<div>After 3 seconds I don't move up.</div> 
  
 

最满意答案

你也可以动画最大高度 - 只要确保中间的最大高度比最高的衰落div高

#test {
  opacity: 0;
  -webkit-animation: fadeinout 3s;
  /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadeinout 3s;
  /* Firefox < 16 */
  -ms-animation: fadeinout 3s;
  /* Internet Explorer */
  -o-animation: fadeinout 3s;
  /* Opera < 12.1 */
  animation: fadeinout 3s;
    max-height: 0;
}

@keyframes fadeinout {
  from {
    max-height: 0;
    opacity: 0;
  }
  50% {
    opacity: 1;
    max-height: 5em;
  }
  to {
    opacity: 0;
    max-height: 0;
  }
} 
  
<div id="test">hide me after 3 seconds</div>
<div>After 3 seconds I don't move up.</div> 
  
 

You could also animate the max-height - just make sure that the middle max-height is taller than your tallest fading div

#test {
  opacity: 0;
  -webkit-animation: fadeinout 3s;
  /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadeinout 3s;
  /* Firefox < 16 */
  -ms-animation: fadeinout 3s;
  /* Internet Explorer */
  -o-animation: fadeinout 3s;
  /* Opera < 12.1 */
  animation: fadeinout 3s;
    max-height: 0;
}

@keyframes fadeinout {
  from {
    max-height: 0;
    opacity: 0;
  }
  50% {
    opacity: 1;
    max-height: 5em;
  }
  to {
    opacity: 0;
    max-height: 0;
  }
} 
  
<div id="test">hide me after 3 seconds</div>
<div>After 3 seconds I don't move up.</div> 
  
 

更多推荐

本文发布于:2023-08-06 14:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   移除   动画   div   CSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!