动画和更改两个不同的图像

时间:2018-09-20 06:48:50

标签: html css css3 css-animations

有两个图像,第一个是船,第二个是飞机。理想的结果是:船从左到右进行动画处理,这时飞机被隐藏了。当船到达屏幕中间时,它消失了,飞机出现了。这种变化应该顺利进行。

.image1 {
  width: 259px;
  height: 259px;
  display: block;
  position: absolute;
  bottom: 135px;
  margin: auto;
  @include transition(all 1.2s);
  background-size: contain;
  -webkit-animation: helicopter-move-one 19s linear infinite;
  animation: helicopter-move-one 19s linear infinite;
  opacity: 1;
}

@-webkit-keyframes helicopter-move-one {
  0% {
    left: -300px;
  }
  60% {
    opacity: 0;
  }
  100% {
    left: 110%;
  }
}

@keyframes helicopter-move-one {
  0% {
    left: -300px;
    display: block;
  }
  59% {
    display: none;
  }
  60% {
    display: none;
  }
  100% {
    left: 110%;
  }
}
<div class="outer">
  <div class="image1"><img src="" alt="boat"></div>
  <div class="image2"><img src="" alt="plane"></div>
</div>

1 个答案:

答案 0 :(得分:2)

由于我没有您的图片,因此我正在使用狗。在这种情况下,“ ”的理想结果是:成年狗从左到右进行动画处理,这时小狗被隐藏了。成年狗到达屏幕中间时,它消失了,小狗也出现了。这种变化应该平稳进行。 ”,请注意,display不能设置动画。您需要设置opacity的动画。

img {
  width: 150px;
  height: 150px;
}

[class ^="image"] {
  width: 150px;
  height: 150px;
  display: block;
  position: absolute;
  left: 0;
  right: 0;
  background-size: contain;
}

.image1 {
  z-index: 2;
  animation: daAnimation1 19s linear infinite;
}
.image2 {
  z-index: 1;
  margin: auto;
  left: 0;
  right: 0;
  opacity: 0;
  animation: daAnimation2 19s linear infinite;
}

@keyframes daAnimation1 {
  0% {
    left: -150px;
    opacity: 1;
  }
  45% {
    left: calc(50vw - 75px);
    opacity: 1;
  }
  50% {
    left: calc(50vw - 75px);
    opacity: 0;
  }
  100% {
    left: 110%;
    opacity: 0;
  }
}

@keyframes daAnimation2 {
  0% {
    opacity: 0;
  }
  45% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 1;
  }
}
<div class="outer">
  <div class="image1"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/darwin300.jpg" alt="adult dog"></div>
  <div class="image2"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppyBeagle300.jpg" alt="puppy"></div>
</div>

我希望这能回答您的问题。

更新:这是@Danish评论的答案(见下文)

img {
  width: 150px;
  height: 150px;
}

[class ^="image"] {
  position:absolute;
  background-size: contain;
}

.image1 {
  z-index: 2;
  opacity: 1;
  animation: daAnimation1 19s linear infinite;
}
.image2 {
  z-index: 1;
  opacity: 1;
  
}

.outer{
  width: 150px;
  height: 150px;
  display: block;
  position: absolute;
  animation: OuterAnimation 19s linear infinite;
}

@keyframes OuterAnimation{
 0% {
    left: -150px;
  }
 
  100% {
    left: 110%;
  }
}

@keyframes daAnimation1 {
  0% {
    opacity: 1;
  }
  45% {
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}
<div class="outer">
  <div class="image1"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/darwin300.jpg" alt="adult dog"></div>
  <div class="image2"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/puppyBeagle300.jpg" alt="puppy"></div>
</div>