CSS 淡出并保持不变

don*_*don 3 html css fade css-animations

是否有一种仅 CSS 的方法可以淡出此文本,如下所示,并在动画完成后使其保持隐藏状态?它当前淡出,然后再次出现。我尝试添加display: none(以及height: 0px,这并不是我真正想要的),但问题仍然存在 - 一旦动画完成,它就会重新出现。

很高兴使用一些 JavaScript 来做到这一点(有一个堆栈溢出答案解释了如何侦听动画事件的结束),但纯 CSS 是首选。

.fade-out {
  animation: fadeOut ease 5s;
  -webkit-animation: fadeOut ease 5s;
  -moz-animation: fadeOut ease 5s;
  -o-animation: fadeOut ease 5s;
  -ms-animation: fadeOut ease 5s;
}

@keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@-moz-keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@-webkit-keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@-o-keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@-ms-keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)
<h1 class="fade-out">hello</h1>
Run Code Online (Sandbox Code Playgroud)

Joh*_*nes 11

用于animation-fill-mode: forwards;该目的:

.fade-out {
  animation: fadeOut ease 5s;
  animation-fill-mode: forwards;
}

@keyframes fadeOut {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)
<h1 class="fade-out">hello</h1>
Run Code Online (Sandbox Code Playgroud)