我有以下HMTL
和CSS
:
.parent{
height: 10%;
width: 100%;
float: left;
}
.content1{
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2{
height: 100%;
width: 20%;
background-color: red;
float: left;
}
.content3{
height: 100%;
width: 20%;
background-color:yellow;
float: left;
}
.content4{
height: 100%;
width: 20%;
background-color: green;
float: left;
}
.content5{
height: 100%;
width: 20%;
background-color: orange;
float: left;
}
.parent {
animation-name: animation_01;
animation-duration:5s;
animation-iteration-count:1;
animation-fill-mode: forwards;
}
@keyframes animation_01 {
0% {
opacity: 0
}
20% {
opacity: 1
}
40% {
opacity: 0
}
60% {
opacity: 1
}
80% {
opacity: 0
}
100% {
opacity: 1
}
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
此代码本身可以正常工作。
您也可以在 JSfiddle here中找到它。
我现在的目标是将content1
到content5
依次显示。因此,我想在CSS的opacity
中使用animation
函数。但是,我仍无法弄清楚如何为单个内容使用opacity
,因为现在动画仅针对所有五个内容组合运行。
您知道是否可以在@keyframes
中使此动画正常工作吗?还是像示例here中一样需要javascript / jQuery?
答案 0 :(得分:2)
您需要为每个div设置动画。您可以对所有动画使用相同的动画。对于每个div集startKey
。这是您在问题中定义的动画的示例:
animation-delay
.parent {
height: 10%;
width: 100%;
float: left;
}
.parent div {
animation-name: animation_01;
animation-duration: 5s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
opacity: 0;
}
.content1 {
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2 {
height: 100%;
width: 20%;
background-color: red;
float: left;
animation-delay: 1s;
}
.content3 {
height: 100%;
width: 20%;
background-color: yellow;
float: left;
animation-delay: 2s;
}
.content4 {
height: 100%;
width: 20%;
background-color: green;
float: left;
animation-delay: 3s;
}
.content5 {
height: 100%;
width: 20%;
background-color: orange;
float: left;
animation-delay: 4s;
}
.parent {}
@keyframes animation_01 {
0% {
opacity: 0
}
20% {
opacity: 1
}
40% {
opacity: 0
}
60% {
opacity: 1
}
80% {
opacity: 0
}
100% {
opacity: 1
}
}
}
,或者如果您希望每个项目仅显示1秒钟,例如:
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
.parent {
height: 10%;
width: 100%;
float: left;
}
.parent div {
animation-name: animation_01;
animation-duration: 2s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
opacity: 0;
}
.content1 {
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2 {
height: 100%;
width: 20%;
background-color: red;
float: left;
animation-delay: 1s;
}
.content3 {
height: 100%;
width: 20%;
background-color: yellow;
float: left;
animation-delay: 2s;
}
.content4 {
height: 100%;
width: 20%;
background-color: green;
float: left;
animation-delay: 3s;
}
.content5 {
height: 100%;
width: 20%;
background-color: orange;
float: left;
animation-delay: 4s;
}
.parent {}
@keyframes animation_01 {
0% {
opacity: 0
}
50% {
opacity: 1
}
100% {
opacity: 0
}
}
}
更新
要让他们一个接一个地显示,请设置父项<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>
和子项position:relative
position: absolute
.parent {
height: 10%;
width: 100%;
float: left;
position: relative;
}
.parent div {
animation-name: animation_01;
animation-duration: 2s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
opacity: 0;
}
.content1 {
height: 100%;
width: 20%;
background-color: blue;
position: absolute;
}
.content2 {
height: 100%;
width: 20%;
background-color: red;
animation-delay: 1s;
position: absolute;
}
.content3 {
height: 100%;
width: 20%;
background-color: yellow;
animation-delay: 2s;
position: absolute;
}
.content4 {
height: 100%;
width: 20%;
background-color: green;
animation-delay: 3s;
position: absolute;
}
.content5 {
height: 100%;
width: 20%;
background-color: orange;
animation-delay: 4s;
}
.parent {}
@keyframes animation_01 {
0% {
opacity: 0
}
50% {
opacity: 1
}
100% {
opacity: 0
}
}
}
答案 1 :(得分:1)
这个想法是将动画添加到子元素而不是父元素上,并添加animation-delay
来延迟每个动画的开始时间(使用sass时这要容易得多)
.parent{
height: 10%;
width: 100%;
float: left;
}
.content1{
height: 100%;
width: 20%;
background-color: blue;
float: left;
}
.content2{
height: 100%;
width: 20%;
background-color: red;
float: left;
animation-delay:.4s;
}
.content3{
height: 100%;
width: 20%;
background-color:yellow;
float: left;
animation-delay:.8s;
}
.content4{
height: 100%;
width: 20%;
background-color: green;
float: left;
animation-delay:1.2s;
}
.content5{
height: 100%;
width: 20%;
background-color: orange;
float: left;
animation-delay:1.6s;
}
.parent div{
animation-name: animation_01;
animation-duration:5s;
animation-iteration-count:1;
animation-fill-mode: forwards;
opacity:0;
}
@keyframes animation_01 {
0% {
opacity: 0;
}
100%{
opacity:1;
}
}
<div class="parent">
<div class="content1">Here goes content1</div>
<div class="content2">Here goes content2</div>
<div class="content3">Here goes content3</div>
<div class="content4">Here goes content4</div>
<div class="content5">Here goes content5</div>
</div>