Отображать один контент за другим, используя ключевые кадры CSS - PullRequest
0 голосов
/ 19 ноября 2018

У меня есть следующие 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 здесь .


Моя цель теперь состоит в том, чтобы отображать content1 до content5 один за другим,Поэтому я хотел использовать функцию opacity в animation CSS.Однако я пока не мог понять, как я могу использовать opacity для отдельного контента, потому что сейчас анимация запускается только для всех пяти контентов вместе взятых.

Знаете ли вы, возможно ли заставить эту анимацию работатьв пределах @keyframes или мне нужен javascript / jQuery, как в примере здесь ?

Ответы [ 2 ]

0 голосов
/ 19 ноября 2018

Вам нужно оживить каждый div. Вы можете использовать одну и ту же анимацию для всех. Для каждого набора div 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
  }
}


}
<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>

или если вы хотите, чтобы каждый элемент отображался только в течение 1 секунды, например:

.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>

UPDATE

Чтобы показать их друг над другом, установите родительский элемент 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
  }
}


}
<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>
0 голосов
/ 19 ноября 2018

Идея состоит в том, чтобы добавить анимацию к дочернему элементу, а не к родительскому элементу, и добавить 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>
...