css - 使用 CSS3 飞行路径的对象的无限动画

标签 css css-animations

美好的一天,我们的任务是制作飞机绕路径飞行的动画。我决定利用 CSS3 中的机会。但我所取得的只是一个动画周期。飞机绕路径飞行一圈,动画停止。我尝试使用 animation-iteration-countinfinite ,但我得到的只是一架飞机在困惑的方向上飞行。下面是我的代码,请告诉我如何循环这个动画,让飞机不停地绕圈飞行。

代码

.wrap {
  margin: 100px;
}

.route {
  height: 200px;
  width: 400px;
  border: 3px dotted #000;
  position: relative;
}

.plane {
  position: absolute;
  bottom: -13px;
  left: 100%;
  animation-iteration-count: 3;
  animation: flyLeft 1.5s linear forwards, rotatePlane 0.5s linear 1.5s forwards, flyUp 1s linear forwards 2s, RotateRight 0.5s linear 2.8s forwards, MoveRight 3s linear forwards 3s, RotateDown 1s linear 6s forwards, flyDown 1s linear forwards 7s, RotateLeft 1s linear 7.8s forwards;
}

@keyframes flyLeft {
  100% {
    left: -14px;
  }
}

@keyframes rotatePlane {
  100% {
    transform: rotateZ(90deg);
  }
}

@keyframes flyUp {
  100% {
    bottom: 100%;
  }
}

@keyframes RotateRight {
  0% {
    transform: rotateZ(90deg);
  }
  100% {
    transform: rotateZ(180deg);
  }
}

@keyframes MoveRight {
  0% {
    left: -14px;
  }
  100% {
    left: 380px;
  }
}

@keyframes RotateDown {
  0% {
    transform: rotateZ(180deg);
  }
  100% {
    transform: rotateZ(270deg);
  }
}

@keyframes flyDown {
  0% {
    bottom: 100%;
  }
  100% {
    bottom: -8%;
  }
}

@keyframes RotateLeft {
  0% {
    transform: rotateZ(270deg);
  }
  100% {
    transform: rotateZ(360deg);
  }
}
<div class="wrap">
  <div class="route">
    <img class="plane" src="http://p36099-290-14699.s290.upress.link/wp-content/uploads/2020/05/plane.png">
  </div>
</div>

最佳答案

您需要将所有动画包装在一个 @keyframes 中CSS at 规则可轻松进行重复。下面是一个工作解决方案,它将所有动画包装在一个 @keyframes 中.

.wrap {
  margin: 100px;
}

.route {
  height: 200px;
  width: 400px;
  border: 3px dotted #000;
  position: relative;
}

.plane {
  position: absolute;
  right: 0;
  bottom: 0;
  transform: translate(50%, 50%);
  animation: travelRoundTheBorder 10s linear infinite;
}

@keyframes travelRoundTheBorder {
  30% {
    bottom: 0;
    right: 100%;
    transform: translate(50%, 50%);
  }
  
  32.5% {
    bottom: 0;
    right: 100%;
    transform: translate(50%, 50%) rotate(90deg);
  }
  
  47.5% {
    right: 100%;
    bottom: 100%;
    transform: translate(50%, 50%) rotate(90deg);
  }
  
  50% {
    right: 100%;
    bottom: 100%;
    transform: translate(50%, 50%) rotate(180deg);
  }
  
  80% {
    right: 0;
    bottom: 100%;
    transform: translate(50%, 50%) rotate(180deg);
  }
  
  82.5% {
    right: 0;
    bottom: 100%;
    transform: translate(50%, 50%) rotate(270deg);
  }
  
  97.5% {
    right: 0;
    bottom: 0;
    transform: translate(50%, 50%) rotate(270deg);
  }
  
  100% {
    right: 0;
    bottom: 0;
    transform: translate(50%, 50%) rotate(360deg);
  }
}
<div class="wrap">
  <div class="route">
    <img class="plane" src="http://p36099-290-14699.s290.upress.link/wp-content/uploads/2020/05/plane.png">
  </div>
</div>

关于css - 使用 CSS3 飞行路径的对象的无限动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61770471/

相关文章:

javascript - 禁用链接直到所有动画停止并再次启用它们

css,不同类的反向转换

css - 爆炸粒子 : Animating with CSS

css 菜单动画没有任何反应

css - iPad Pro 的正确媒体查询是什么?

html - 我需要一个解决方案来处理 CSS3 中元素的 id 内的两个选择器

css - 我无法链接到外部 css 文件

html - chrome 中的不同文本位置。 IE 和 FF

html - 根据父元素类使用不同的@keyframes

html - 用另一个元素包裹时,CSS 动画无法正常工作