Has a different way of doing using linear-gradiente
, so no more than a tag or use pseudo-element.
Imagine you have one linear gradient as background
of a container, and that this gradient is divided equally with 50% of the width with one color and the other 50% with another color.
Now with the background-size
you put that background
with 200% of the width
of container. Thus each half of the gradient now occupies 100% of the width of the container.
Now with background-position
and the @keyframes
we do the background
walk 100% to one side, showing 100% of the other color that was "hidden" outside the container.
The animation
On the property animation
it is composed as follows in shorthand:
animation: name duration timing-function delay iteration-count direction fill-mode;
Or in the Longhand (values initial
)
animation-name: none
animation-duration: 0s
animation-timing-function: ease
animation-delay: 0s
animation-iteration-count: 1
animation-direction: normal
animation-fill-mode: none
animation-play-state: running
In your case you need to focus on name
(name of the animation declared in @keyframes
), duration
(have you want it to last the animation 6s) and iteration-count
(how many times the animation will repeat itself infinite
"eternal loop") In this documentation of Mozilla you can find out more: https://developer.mozilla.org/en-US/docs/Web/CSS/animation
After defined this we will work on the animation intervals built within the at-Rule @keyframes
. As I want you to have a 1s interval first to see the 100% empty bar at the beginning and then a 1s interval for you to see the 100% full bar at the end I will repeat some values at the beginning and at the end of the @keyframes
The comments are in the code below:
OBS: as the animation has 6 second I will divide 100 by 6, so we have 6 16.66% intervals representing each second of animation. In the first second and in the last second the bar is stopped for 1 second. If you do not want this effect just put this way. So the full bar straight, without a "delay" at the beginning and at the end for you to see the two full bar states:
/* estado inicial da barra */
0% {
background-position: 99% 0;
}
/* estado final da barra */
100% {
background-position: 0 0;
}
To better understand how the animation fractionation works and the creation of this "delay" see the comments I left in the code below:
.barra {
height: 16px;
width: 50%;
border-radius: 8px;
margin: 10px auto;
/* gradiente com 2 cores */
background-image: linear-gradient(to right, #bada55 0, #bada55 50%, gray 50%);
/* agradiente com 200% da largura do container 101% pra cada cor */
background-size: 202% 100%;
/* move o backgrount para mostrar apenas a primeira cor */
background-position: 99% 0;
/* animação que move o background para mostrar a segunda cor */
animation: anima 6s ease-in-out infinite;
/* coloquei 500ms de delay para vc poder ver a barra 100% "vazia" antes de iniciar a animação */
}
@keyframes anima {
0% {
/* movemo o background para mostrar apenas 100% de uma cor */
background-position: 99% 0;
}
/* do 0% do tempo da animação até 16,66% do tempo a barra permance parada */
16.66% {
background-position: 99% 0;
}
/* esse intervalo representa 4 segundos do tempo da animação até a barra fica 100% cheia */
83.34% {
background-position: 0 0;
}
/* esse intervalo de 83,34% a 100% representa 1 segundo da animação e a barra fica cheia por 1s antes de reiniciar*/
100% {
/* de 75% a 100% eu repito o estado da propriedade para vc ter um tempo de ver a barra 100% completa antes de reiniciar a animação */
background-position: 0 0;
}
}
<div class="barra"></div>
An image to better explain the technique.
The blue border is the container, and the bottom moving is the background
. The background
has 200% the width of the parent, so each color that has 50% of the gradient actually gets 100% of the parent’s width. Then adjusting the horizontal value of the parent background-position
in the @keyframes
it is possible to move the bar, giving the impression that is completing the container.
Browser support: According to the website https://caniuse.com/ this model works from IE10 up, Chrome, Fire Fox, Safari, etc...
Only with . css was this the closest I could find. https://codepen.io/baletsa/pen/omaHe
– PauloHDSousa