使用关键帧时图像不褪色

时间:2019-06-29 22:20:10

标签: html css css-animations keyframe

我正在尝试使用关键帧淡入图像。我遇到的问题是图像出现然后消失,然后关键帧开始。下面是我的html。

<div class="col-lg-6 col-md-2 col-sm-12">
  <img class="iphone_image" src="images/iphone6.png" alt="iphone-mockup">
</div>

以下是我的CSS和关键帧:

.iphone_image {
  background-image: url("images/iphone6.png");
  width: 60%;
  transform: rotate(25deg);
  animation-name: image-appear;
  animation-duration: 10s;
  animation-delay: 5s;
  animation-iteration-count: 1;
  animation-timing-function: ease-in;
}
@keyframes image-appear {
  from {opacity:0;}
  to {opacity: 1;}
}

我想发生的是网页加载时到图像出现时的过渡。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您必须将opacity: 0;添加到图像的默认状态:

.iphone_image {
  background-image: url("images/iphone6.png");
  width: 60%;
  transform: rotate(25deg);
  animation-name: image-appear;
  animation-duration: 3s;
  animation-delay: 1s;
  animation-iteration-count: 1;
  animation-timing-function: ease-in;
  opacity: 0;
}

@keyframes image-appear {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<div class="col-lg-6 col-md-2 col-sm-12">
  <img class="iphone_image" src="http://lorempixel.com/400/200/" alt="iphone-mockup">
</div>