为什么 CSS3 动画不起作用?

新手上路,请多包涵

有人可以帮我找出为什么 <h5> 元素上的动画不起作用吗?

 #hero h5 {
  animation: fadein 2s;
  -moz-animation: fadein 2s; /* Firefox */
  -webkit-animation: fadein 2s; /* Safari and Chrome */
  -o-animation: fadein 2s; /* Opera */
  font-weight: strong;
  font-size: 28px;
}
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<section id="hero">
    <div class="content">
        <div class="container">
            <div class="row">
                <h5>Lorem Ipsum Demo Title</h5>
            </div><!-- row -->
        </div> <!-- container -->
    </div> <!-- content -->
</section><!-- section -->

原文由 Morgari 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 483
2 个回答

您正在代码中调用 fadein 动画,但您尚未在任何地方定义它。

CSS3 动画是用 @keyframes 规则定义的。有关 CSS3 动画的更多信息,请参见 此处

添加以下CSS:

 @keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

 #hero h5 {
  animation: fadein 2s;
  -moz-animation: fadein 2s; /* Firefox */
  -webkit-animation: fadein 2s; /* Safari and Chrome */
  -o-animation: fadein 2s; /* Opera */
  font-weight: strong;
  font-size: 28px;
}

@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<section id="hero">
  <div class="content">
    <div class="container">
      <div class="row">
        <h5>Lorem Ipsum Demo Title</h5>
      </div><!-- row -->
    </div> <!-- container -->
  </div> <!-- content -->
</section><!-- section -->

原文由 Mohammad Usman 发布,翻译遵循 CC BY-SA 3.0 许可协议

 #hero h5 {
     font-weight: strong;
     font-size: 28px;
 -webkit-animation-delay: 0.7s;
  -moz-animation-delay: 0.7s;
  animation-delay: 0.7s;
}

@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

.fade-in {
  opacity:0;
  -webkit-animation:fadeIn ease-in 1;
  -moz-animation:fadeIn ease-in 1;
  animation:fadeIn ease-in 1;

  -webkit-animation-fill-mode:forwards;
  -moz-animation-fill-mode:forwards;
  animation-fill-mode:forwards;

  -webkit-animation-duration:1s;
  -moz-animation-duration:1s;
  animation-duration:1s;
}
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<section id="hero">
        <div class="content">
            <div class="container">
                <div class="row">
                <h5 class="fade-in">Lorem Ipsum Demo Title</h5>

                </div><!-- row -->
            </div> <!-- container -->
        </div> <!-- content -->
</section><!-- section -->


原文由 Jatin Kalra 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题