In CSS3, you can create animations and create simple animations without JavaScript

In this article, I will show you how to use CSS animation to achieve the motion of a ball

So let’s see what happens when we write it

Let’s get started!

First we need to create a root element to hold the ball.

    <div class="root">
        <! - ball - >
        <div class="ball"></div>
    </div>
Copy the code

Style containers and balls.

.root {
  background-color: #34495e;
  width: 270px;
  height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.root .ball {
  width: 50px;
  height: 50px;
  background: orange;
  border-radius: 50%;
  position: absolute;
}
Copy the code

Next, we create an animation that moves the ball.

@keyframes ball {
    100%{
        transform: translateY(-130px); }}Copy the code

Attach the animation rule to the ball element and add it to the Ball class.

.ball {
        animation-name: ball;
        animation-iteration-count: infinite;
        animation-direction: alternate-reverse;
        animation-duration: 1s;
}
Copy the code

As you can see, the ball is now animated, but this is not realistic enough, so let’s add a shadow effect to it.

<div class="root">
        <! - ball - >
        <div class="ball"></div>
        <! -- Ball shadow -->
        <div class="shadow"></div>
</div>
Copy the code

Set the style.

.root .shadow {
  height: 20px;
  width: 100px;
  background: gray;
  position: absolute;
  border-radius: 50%;
  bottom: 140px;
}
Copy the code

Also, we need to create an animation of the shadow, scale it, and mount it to the element.

@keyframes shadow {
    100%{
        transform: scale(.5);
    }
}
.shadow{
        animation-name: shadow;
        animation-iteration-count: infinite;
        animation-direction: alternate-reverse;
        animation-duration: 1s;
}
Copy the code

There we go. Let’s see what it looks like!

The overall code

HTML

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
    <div class="root">
        <! - ball - >
        <div class="ball"></div>
        <! -- Ball shadow -->
        <div class="shadow"></div>
    </div>
</body>
</html>
Copy the code

CSS

.root {
  background-color: #34495e;
  width: 270px;
  height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.root .ball {
  width: 50px;
  height: 50px;
  background: orange;
  position: absolute;
  border-radius: 50%;
  animation-name: ball;
  animation-iteration-count: infinite;
  animation-direction: alternate-reverse;
  animation-duration: 1s;
}
.root .shadow {
  height: 20px;
  width: 100px;
  background: gray;
  position: absolute;
  border-radius: 50%;
  bottom: 140px;
  animation-name: shadow;
  animation-iteration-count: infinite;
  animation-direction: alternate-reverse;
  animation-duration: 1s;
}

@keyframes ball {
  100% {
    transform: translateY(-130px); }}@keyframes shadow {
  100% {
    transform: scale(0.5); }}Copy the code