Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Controlling Animation Iteration and Direction | Advanced CSS Animations
Advanced CSS Techniques

book
Controlling Animation Iteration and Direction

animation-iteration-count property

We can control how many times an animation is supposed to be played. animation-iteration-count sets the number of times the animation will be repeated.

css
animation-iteration-count: number_without_units

We also can specify the keyword infinite as a value. So, the animation will always process. Let's consider the following example and check how we can affect the number of animations:

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="container">
<div class="item">
<p>Without iteration count</p>
<img
class="first-image"
src="https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/advanced+css+techniques/animations+section/planet-earth+(1).png"
alt="Earth"
width="100px"
/>
</div>

<div class="item">
<p>With iteration count (5)</p>
<img
class="second-image"
src="https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/advanced+css+techniques/animations+section/planet-earth+(1).png"
alt="Earth"
width="100px"
/>
</div>

<div class="item">
<p>With iteration count (infinite)</p>
<img
class="third-image"
src="https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/advanced+css+techniques/animations+section/planet-earth+(1).png"
alt="Earth"
width="100px"
/>
</div>
</div>
</body>
</html>

animation-direction property

animation-direction controls the direction of the animation. It determines whether an animation should play forwards, backwards, or alternate forward and backward.

css
animation-direction: normal | reverse | alternate | alternate-reverse
  • normal - default value. The animation will play forwards from the beginning to the end;

  • reverse - The animation will play backwards from the end to the beginning;

  • alternate - The animation will play forwards and then backwards, alternating between the two directions;

  • alternate-reverse - The animation will play backwards and forwards, alternating between the two directions.

Let's see what effect we can achieve in the following example. We will have an image in which the default behavior is to repeatedly move from the left side to the right. However, we can change its behavior. We will test all four values for the animation-direction property.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="container">
<p>animation-direction: normal</p>
<img
class="image-one"
src="https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/advanced+css+techniques/animations+section/example+for+the+animation+direction+property.png"
alt="dog"
width="100px"
/>
</div>
<div class="container">
<p>animation-direction: reverse</p>
<img
class="image-two"
src="https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/advanced+css+techniques/animations+section/example+for+the+animation+direction+property.png"
alt="dog"
width="100px"
/>
</div>
<div class="container">
<p>animation-direction: alternate</p>
<img
class="image-three"
src="https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/advanced+css+techniques/animations+section/example+for+the+animation+direction+property.png"
alt="dog"
width="100px"
/>
</div>
<div class="container">
<p>animation-direction: alternate-reverse</p>
<img
class="image-four"
src="https://codefinity-content-media.s3.eu-west-1.amazonaws.com/code-1/advanced+css+techniques/animations+section/example+for+the+animation+direction+property.png"
alt="dog"
width="100px"
/>
</div>
</body>
</html>

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 3

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

We use cookies to make your experience better!
some-alt