I want to obtain blinking dots one after one in one direction and then blinking have to go back, and all have to happen in the loop. I have created CSS code with Javascript according to first five dots. What can I do to make shorter all reqired code in CSS? Also I want to make not visible black backgrounds of the dots during blinking.
for (let i = 0; i < 40; i++) {
const dots = document.createElement("div");
document.body.appendChild(dots).classList.add("dots");
}
body {
background-color: #737373;
display: flex;
justify-content: space-evenly;
align-items: center;
min-height: 100vh;
overflow: hidden;
}
.dots {
background-color: rgb(0, 0, 0);
width: 20px;
height: 20px;
border-radius: 50%;
}
.dots:nth-of-type(1)::after {
content: "";
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
animation: bright 0.3s linear;
background-color: rgb(255, 255, 255);
box-shadow: 0 0 3px 3px rgb(255, 255, 255),
0 0 5px 5px rgb(255, 255, 255), 0 0 12px 12px rgb(255, 255, 255);
opacity: 0;
animation-delay: 0s;
}
.dots:nth-of-type(2)::after {
content: "";
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
animation: bright 0.3s linear;
background-color: rgb(255, 255, 255);
box-shadow: 0 0 3px 3px rgb(255, 255, 255),
0 0 5px 5px rgb(255, 255, 255), 0 0 12px 12px rgb(255, 255, 255);
opacity: 0;
animation-delay: 0.25s;
}
.dots:nth-of-type(3)::after {
content: "";
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
animation: bright 0.3s linear;
background-color: rgb(255, 255, 255);
box-shadow: 0 0 3px 3px rgb(255, 255, 255),
0 0 5px 5px rgb(255, 255, 255), 0 0 12px 12px rgb(255, 255, 255);
opacity: 0;
animation-delay: 0.5s;
}
.dots:nth-of-type(4)::after {
content: "";
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
animation: bright 0.3s linear;
background-color: rgb(255, 255, 255);
box-shadow: 0 0 3px 3px rgb(255, 255, 255),
0 0 5px 5px rgb(255, 255, 255), 0 0 12px 12px rgb(255, 255, 255);
opacity: 0;
animation-delay: 0.75s;
}
.dots:nth-of-type(5)::after {
content: "";
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
animation: bright 0.3s linear;
background-color: rgb(255, 255, 255);
box-shadow: 0 0 3px 3px rgb(255, 255, 255),
0 0 5px 5px rgb(255, 255, 255), 0 0 12px 12px rgb(255, 255, 255);
opacity: 0;
animation-delay: 1s;
}
@keyframes bright {
0%,
100% {
opacity: 0;
}
50% {
opacity: 1;
}
}
2
Answers
CSS can do this for you without needing to define the delays for every circle.
In this snippet, a before pseudo element moves along the circles in steps (see definition of CSS animation steps for more info) with color the same as the background. It will therefore briefly ‘hide’ the blackness of a circle.
The after pseudo element also moves along in the same steps, but has the blinking part as well.
This way it looks as though each circle is being individually ‘blinked’ in turn.
The CSS animation ‘alternate’ value is used to make the animations reverse and they are made infinite.
Note: CSS variables are used so that the calculation that has to be done to get the starting position of the first circle etc can be found easily using calc. In particular, in this case the justify-content value will determine what the gap is.
Also, I have kept the same HTML structure as in the question but would recommend using a container for the circles rather than body being the direct parent.
An idea with a single element and no JS
And if you want an overflowing effect for the shadow, you can update like below: