css - 如何定义css动画?

标签 css animation css-animations

我已经编写了这段代码来使矩形像汽车一样从左向右移动,但这不起作用。我认为 @keyframes 示例有问题

* {
  margin: auto;
  padding: 0px;
}

body {
  background-color: lightgreen;
}

.main {
  height: 100%;
  width: 100%;
  border: 1px solid black;
}

.road {
  height: 300px;
  width: 100%;
  background-color: black;
  margin-top: 25%;
}

@keyframes example {
  from {
    top: 20%;
    left: 80%
  }
  20% {
    top: 20%;
    left: 60%
  }
  40% {
    top: 20%;
    left: 50%
  }
  60% {
    top: 20%;
    left: 30%
  }
  80% {
    top: 20%;
    left: 10%
  }
  to {
    top: 20%;
    left: 0%
  }
}

@-webkit-keyframes example {
  0% {
    top: 20%;
    left: 80%
  }
  20% {
    top: 20%;
    left: 60%
  }
  40% {
    top: 20%;
    left: 50%
  }
  60% {
    top: 20%;
    left: 30%
  }
  80% {
    top: 20%;
    left: 10%
  }
  100% {
    top: 20%;
    left: 0%
  }
}

@-moz-keyframes example {
  0% {
    top: 20%;
    left: 80%
  }
  20% {
    top: 20%;
    left: 60%
  }
  40% {
    top: 20%;
    left: 50%
  }
  60% {
    top: 20%;
    left: 30%
  }
  80% {
    top: 20%;
    left: 10%
  }
  100% {
    top: 20%;
    left: 0%
  }
}

.car {
  height: 100px;
  width: 200px;
  background-color: red;
  font-size: 50px;
  color: black;
  text-align: center;
  vertical-align: middle;
  margin-top: 100px;
  animation-name: example;
  animation-duration: 10s;
  animation-iteration-count: infinite;
  -webkit-animation-name: example;
  -webkit-animation-duration: 10s;
  -webkit-animation-iteration-count: infinite;
  -moz-animation-name: example;
  -moz-animation-duration: 10s;
  -moz-animation-iteration-count: infinite;
}

最佳答案

您需要在 .car 上使用 position: relative 才能像那样显示。

.car {
    position: relative;
    height:100px;
    width:200px;
    background-color: red;
    font-size: 50px;
    color:black;
    text-align: center;
    vertical-align: middle;
    margin-top: 100px;

    animation-name:example;
    animation-duration: 10s;
    animation-iteration-count: infinite;

    -webkit-animation-name: example;
    -webkit-animation-duration: 10s;
    -webkit-animation-iteration-count: infinite;

    -moz-animation-name: example;
    -moz-animation-duration: 10s;
    -moz-animation-iteration-count: infinite;
}

关于css - 如何定义css动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016214/

相关文章:

css - 尝试重新创建像 Stripe 这样的摇动动画

html - bannermover 的第一个 img 没有直接跟在 bannermove 的最后一个 img 之后

html - 使用 CSS3 动画更改文本?

html - 如何阻止图像将单元格的高度拉伸(stretch)到超出限制

html - 如何使用 css 选择 svg 图像的颜色填充? (不是内联 svg)

css - 在 1024 分辨率下不显示粘性 header ?

javascript - 滚动时动画不起作用 - JQuery

javascript - 如何用动画将列表项移动到顶部?

javascript - 如何在 Angular 中设置未知高度的动画?

css - 如何将 h1 居中放置在正文中