在动画之后,仅使用css隐藏/删除div从html占用的空间

时间:2018-03-13 13:51:50

标签: html css 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;
  }
}
&#13;
<div id="test">hide me after 3 seconds</div>
<div>After 3 seconds I don't move up.</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您还可以设置最大高度的动画 - 只需确保中间最大高度高于最高的渐变高度

#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>