如何正确使用关键帧动画?

时间:2020-09-03 13:05:13

标签: html css

我正在尝试使div在页面加载时一个接一个地显示。问题是该设置仅在我向div选择器添加visibility: hidden属性的情况下才有效,而后者又会反转动画。我想念什么?

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73, 73, 73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53, 53, 53);
}

.box1 {
  animation: test 1s;
}

.box2 {
  animation: test 1.1s;
}

.box3 {
  animation: test 1.2s;
}

.box4 {
  animation: test 1.3s;
}

.box5 {
  animation: test 1.4s;
}

@keyframes test {
  from {
    visibility: hidden;
  }
  to {
    visibility: visible;
  }
}
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>

4 个答案:

答案 0 :(得分:1)

您需要为不透明度设置动画以使该项目显示-您可以将长度设置为1%,以便它只是“弹出”而不是淡入。

我还将animation-fill-mode设置为forwards,以便维持最终状态并使用animation-delay来设置每个框弹出之间的时间

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73, 73, 73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53, 53, 53);
}

.box {
  opacity: 0;
  animation: test 0.1s forwards;
}

.box1 {
  animation-delay: 1s;
}

.box2 {
  animation-delay: 1.1s;
}

.box3 {
  animation-delay: 1.2s;
}

.box4 {
  animation-delay: 1.3s;
}

.box5 {
  animation-delay: 1.4s;
}

@keyframes test {
  0% {
    opacity: 0;
  }
  99% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
<div class="box box5"></div>

答案 1 :(得分:0)

请将visibility: hidden;添加到每个

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73,73,73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53,53,53);
}

.box1{
  visibility: hidden;
  animation: test 1s;
}

.box2{
  visibility: hidden;
  animation: test 1.1s;
}

.box3{
  visibility: hidden;
  animation: test 1.2s;
}

.box4{
  visibility: hidden;
  animation: test 1.3s;
}

.box5{
  visibility: hidden;
  animation: test 1.4s;
}

@keyframes test {
  from {
    visibility: hidden;
  }

  to {
    visibility: visible;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./style.css">
  <title>Document</title>
</head>
<body>

  <div class="box1"></div>
  <div class="box2"></div>
  <div class="box3"></div>
  <div class="box4"></div>
  <div class="box5"></div>


</body>
</html>

答案 2 :(得分:0)

由于它是装载机,这是我可以建议您使用高度和宽度的无限装载机:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73,73,73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53,53,53);
}

.box1{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0s infinite;
}

.box2{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.2s infinite;
}

.box3{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.4s infinite;
}

.box4{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.6s infinite;
}

.box5{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.8s infinite;
}

@keyframes test {
  from {
    height: 15vh;
    width: 15vh;
  }

  to {
     height: 0;
    width: 0;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./style.css">
  <title>Document</title>
</head>
<body>

  <div class="box1"></div>
  <div class="box2"></div>
  <div class="box3"></div>
  <div class="box4"></div>
  <div class="box5"></div>


</body>
</html>

答案 3 :(得分:0)

@Pete出色的工作。我使用“缩放”而不是不透明度。谢谢!

    * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73,73,73);
}

div {
  transform: scale(0);
  width: 15vh;
  height: 15vh;
  background-color: rgb(53,53,53);
}

.box {
  animation: test 0.4s forwards;
}

.box1{
  animation-delay: 0.2s;
}

.box2{
  animation-delay: 0.4s;
}

.box3{
  animation-delay: 0.6s;
}

.box4{
  animation-delay: 0.8s;
}

.box5{
  animation-delay: 1s;
}

@keyframes test {
  from {
    transform: scale(0);
  }

  to {
    transform: scale(1, 1);
  }
}
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
<div class="box box5"></div>