我正在try 为两个不同的类制作一些动画,而不是重复@KEYFRAMES属性.使得类的样式将不同,而关键帧动画属性保持不变. 即与两个不同元素相关联的类CLASS_1和CLASS_2.我try 在不分别重复CLASS_1和CLASS_2整个KEYFRAMES属性的情况下拥有@KEYFRAMES(CLASS_1 CLASS_2).

我试着在关键帧后用空格分隔两个类,并使用逗号,这两种情况都没有为两个元素生成动画,但当我在关键帧后放置一个类时就会产生动画.希望获得一种在不重复关键帧属性的情况下包括这两个类的方法.

/* properties of all the elements */

div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-duration: 4;
  animation-iteration-count: infinite;
  /*animation run time*/
}


/* the third and fourth element */

.anim_3,
.anim_4 {
  position: relative;
}


/* for the third element */

.anim_3 {
  animation-name: anim_3;
  animation-delay: 1s;
}


/* for the fourth element */

.anim_4 {
  animation-name: anim_4;
  animation-delay: 2s;
  animation-direction: reverse;
}


/* Animation code of the third and fourth element */

@keyframes anim_3,
anim_4 {
  0% {
    background-color: red;
    left: 0px;
    top: 0px;
  }
  25% {
    background-color: yellow;
    left: 200px;
    top: 0px;
  }
  50% {
    background-color: blue;
    left: 200px;
    top: 200px;
  }
  75% {
    background-color: green;
    left: 0px;
    top: 200px;
  }
  100% {
    background-color: red;
    left: 0px;
    top: 0px;
  }
}
<h1>CSS Animation</h1>
<p>
  <b>Note</b> Both the background-color and the position of the element changes
</p>
<div class="anim_3">Third Animation</div>
<p>
  <b>Note:</b> the animation runs in the reverse direction
</p>
<div class="anim_4">Fourth Animation</div>

推荐答案

你所做的是试图给动画起两个不同的名字.这是不允许的. 我将名字改为动画,并在课程中增加了1秒的时长.

/* properties of all the elements */

div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-duration: 4;
  animation-iteration-count: infinite;
  /*animation run time*/
}


/* the third and fourth element */

.anim_3,
.anim_4 {
  position: relative;
}


/* for the third element */

.anim_3 {
  animation-name: anim;
  animation-delay: 1s;
  animation-duration: 1s;
}


/* for the fourth element */

.anim_4 {
  animation-name: anim;
  animation-delay: 2s;
  animation-direction: reverse;
  animation-duration: 1s;
}


/* Animation code of the third and fourth element */

@keyframes anim {
  0% {
    background-color: red;
    left: 0px;
    top: 0px;
  }
  25% {
    background-color: yellow;
    left: 200px;
    top: 0px;
  }
  50% {
    background-color: blue;
    left: 200px;
    top: 200px;
  }
  75% {
    background-color: green;
    left: 0px;
    top: 200px;
  }
  100% {
    background-color: red;
    left: 0px;
    top: 0px;
  }
}
<h1>CSS Animation</h1>
<p>
  <b>Note</b> Both the background-color and the position of the element changes
</p>
<div class="anim_3">Third Animation</div>
<p>
  <b>Note:</b> the animation runs in the reverse direction
</p>
<div class="anim_4">Fourth Animation</div>

Html相关问答推荐

为什么XPath返回的元素比我预期的要多?

如何使用HTML和css代码在vb.net上打印POS

为什么前端框架可以创建自定义属性,而我却被期望在挖掘之前添加数据

子元素的单一背景

响应网格,响应调整大小以适应父div,保留父div S自己的响应高度

如何在页面太短时使<;img&>缩小而不发生y溢出

渐变进度条位置

为什么 Select 元素在带有数据绑定的Blazor上行为怪异?

如何使用css在响应图像后面添加形状?

HTML 邮箱在 Gmail 中无法正确显示

perl hdb 调试器:浏览器以错误的编码显示 UTF-8 源代码

为什么我的网页显示网格区域彼此相邻,而不是我设置它们的方式?

四开:如何右对齐 PDF *和* HTML 中的文本

Flex布局中,当父元素高度较高时,交叉轴上出现额外的空白问题.

如何通过像图像一样的 元素来调整 SVG 图标的大小

CSS 跨度与子元素交替 colored颜色

那边界从何而来?

是否可以为长 huxtable 固定头部?

以 HTML 格式显示的 LaTeX 表格

使用 CSS 的发光边框动画没有流畅的过渡