David Skx
David Skx

Reputation: 131

How to properly use keyframes animation?

I'm trying to make the divs appear one after another on page load. Problem is this setup only works IF I add a visibility: hidden property to the div selector, which in turn, reverses the animation. What am I missing?

* {
  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>

Upvotes: 2

Views: 97

Answers (4)

Pete
Pete

Reputation: 58412

You need to animate the opacity to make the item appear - you can set the length to 1% so it just "pops" in rather than fades in.

I have also set the animation-fill-mode to forwards so the final state is maintained and used animation-delay to set the time between each box popping in

* {
  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>

Upvotes: 1

David Skx
David Skx

Reputation: 131

@Pete Excellent work. I used 'scale' instead of opacity. Thanks!

    * {
  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>

Upvotes: 0

MaxiGui
MaxiGui

Reputation: 6348

As it is a loader, Here is another I can propose you with height and width, infinite loader:

* {
  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>

Upvotes: 0

Rayees AC
Rayees AC

Reputation: 4659

please add visibility: hidden; to every boxes

* {
  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>

Upvotes: 0

Related Questions