CSS-animationer


Innehållsförteckning

    Visa innehållsförteckning


CSS-animationer

CSS tillåter animering av HTML-element utan att använda JavaScript eller Flash!

CSS

I det här kapitlet kommer du att lära dig om följande egenskaper:

  • @keyframes
  • animation-name
  • animation-duration
  • animation-delay
  • animation-iteration-count
  • animation-direction
  • animation-timing-function
  • animation-fill-mode
  • animation

Webbläsarstöd för animationer

Siffrorna i tabellen anger den första webbläsarversionen som fullt ut stöder egenskapen.

Property
@keyframes 43.0 10.0 16.0 9.0 30.0
animation-name 43.0 10.0 16.0 9.0 30.0
animation-duration 43.0 10.0 16.0 9.0 30.0
animation-delay 43.0 10.0 16.0 9.0 30.0
animation-iteration-count 43.0 10.0 16.0 9.0 30.0
animation-direction 43.0 10.0 16.0 9.0 30.0
animation-timing-function 43.0 10.0 16.0 9.0 30.0
animation-fill-mode 43.0 10.0 16.0 9.0 30.0
animation 43.0 10.0 16.0 9.0 30.0

Vad är CSS-animationer?

En animering låter ett element gradvis ändras från en stil till en annan.

Du kan ändra hur många CSS-egenskaper du vill, hur många gånger du vill.

För att använda CSS-animering måste du först ange några nyckelrutor för animation.

Keyframes innehåller vilka stilar elementet kommer att ha vid vissa tidpunkter.


@keyframes-regeln

När du anger CSS-stilar i @keyframes regeln kommer animationen gradvis att ändras från den nuvarande stilen till den nya stilen vid vissa tidpunkter.

För att få en animation att fungera måste du binda animationen till ett element.

Följande exempel binder "exempel"-animeringen till <div>-elementet. Animeringen kommer att pågå i 4 sekunder, och den kommer gradvis att ändra bakgrundsfärg för elementet <div> från "röd" till "gul":

Exempel

/* The animation code */
@keyframes example {
  from {background-color: red;}
   
to {background-color: yellow;}
}
/* The element to apply the animation to */
div {
  width: 100px;
  height: 100px;
  
background-color: red;	animation-name: example;
  animation-duration: 4s;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

@keyframes example {
  from {background-color: red;}
  to {background-color: yellow;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<div></div>

<p><b>Note:</b> When an animation is finished, it goes back to its original style.</p>

</body>
</html>


Obs! Egenskapen animation-duration definierar hur lång tid en animering ska ta att slutföra. Om egenskapen animation-duration inte anges, ingen animering kommer att inträffa, eftersom standardvärdet är 0s (0 sekunder).

I exemplet ovan har vi specificerat när stilen kommer att ändras genom att använda sökorden "från" och "till" (vilket representerar 0 % (start) och 100 % (slutfört)).

Det är också möjligt att använda procent. Genom att använda procent kan du lägga till så många stil ändras som du vill.

Följande exempel kommer att ändra bakgrundsfärgen för <div> element när animeringen är 25 % klar, 50 % klar och igen när animeringen är 100 % klar:

Exempel

/* The animation code */
@keyframes example
{
  0%   {background-color: red;}
   
25%  {background-color: yellow;}
  50%  {background-color: blue;}
  100% {background-color: green;}
}
/* The element to apply the animation to */
div {
  
width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style>
div {
  width: 100px;
  height: 100px;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

@keyframes example {
  0%   {background-color: red;}
  25%  {background-color: yellow;}
  50%  {background-color: blue;}
  100% {background-color: green;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<div></div>

<p><b>Note:</b> When an animation is finished, it goes back to its original style.</p>

</body>
</html>


Följande exempel kommer att ändra både bakgrundsfärgen och positionen för <div> element när animeringen är 25 % klar, 50 % klar och igen när animeringen är 100 % klar:

Exempel

/* The animation code */
@keyframes example
{
  0%   {background-color:red; left:0px; top:0px;}
   
25%  {background-color:yellow; left:200px; top:0px;}
   
50%  {background-color:blue; left:200px; top:200px;}
   
75%  {background-color:green; left:0px; top:200px;}
   
100% {background-color:red; left:0px; top:0px;}
}
/* The element to apply the animation to */
div {
  
width: 100px;
  height: 100px;
  
position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<div></div>

<p><b>Note:</b> When an animation is finished, it goes back to its original style.</p>

</body>
</html>




Fördröja en animering

Egenskapen animation-delay anger en fördröjning för starten av en animation.

Följande exempel har en fördröjning på 2 sekunder innan animeringen startas:

Exempel

div {
  
width: 100px;
  height: 100px;
  
position: relative;
  background-color: red;
  animation-name: example;
    
animation-duration: 4s;
  animation-delay: 2s;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
  animation-delay: 2s;
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>The animation-delay property specifies a delay for the start of an animation. The following example has a 2 seconds delay before starting the animation:</p>

<div></div>

</body>
</html>


Negativa värden är också tillåtna. Om du använder negativa värden, animeringen kommer att starta som om den redan hade spelat i N sekunder.

I följande exempel kommer animeringen att starta som om den redan hade gjort det spelar i 2 sekunder:

Exempel

div {
  
width: 100px;
  height: 100px;
  
position: relative;
  background-color: red;
  
animation-name: example;
  
animation-duration: 4s;
  animation-delay: -2s;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
  animation-delay: -2s;
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>Using negative values in the animation-delay property: Here, the animation will start as if it had already been playing for 2 seconds:</p>

<div></div>

</body>
</html>



Ställ in hur många gånger en animering ska köras

Egenskapen animation-iteration-count anger hur många gånger en animation ska köras.

Följande exempel kommer att köra animeringen 3 gånger innan den stoppas:

Exempel

div {
  width: 100px;
  height: 100px;
  position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 3;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 3;
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>The animation-iteration-count property specifies the number of times an animation should run. The following example will run the animation 3 times before it stops:</p>

<div></div>

</body>
</html>


Följande exempel använder värdet "oändlig" för att göra animeringen fortsätt för alltid:

Exempel

div {
  
width: 100px;
  height: 100px;
  
position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
    animation-iteration-count: 
infinite;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: infinite;
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>The animation-iteration-count property can be set to infinite to let the animation run for ever:</p>

<div></div>

</body>
</html>



Kör animering i omvänd riktning eller alternativa cykler

Egenskapen animation-direction anger om en animation ska spelas framåt, bakåt eller omväxlande cykler.

Egenskapen animation-direction kan ha följande värden:

normal

- Animationen spelas som vanligt (framåt). Detta är standard

reverse

- Animationen spelas i omvänd riktning (bakåt)

alternate

- Animationen spelas först framåt, sedan bakåt

alternate-reverse

- Animationen spelas först baklänges, sedan framåt

Följande exempel kommer att köra animeringen i omvänd riktning (bakåt):

Exempel

div {
  
width: 100px;
  height: 100px;
  
position: relative;
  background-color: red;
  
animation-name: example;
  
animation-duration: 4s;
  animation-direction: 
reverse;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
  animation-direction: reverse;  
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>The animation-direction property specifies whether an animation should be played forwards, backwards or in alternate cycles. The following example will run the animation in reverse direction (backwards):</p>

<div></div>

</body>
</html>


Följande exempel använder värdet "alternate" för att göra animeringen spring först framåt, sedan bakåt:

Exempel

div {
  
width: 100px;
  height: 100px;
  
position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: 
alternate;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: alternate;  
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>The animation-direction property specifies whether an animation should be played forwards, backwards or in alternate cycles. The following example uses the value "alternate" to make the animation run forwards first, then backwards:</p>

<div></div>

</body>
</html>


Följande exempel använder värdet "alternate-reverse" för att göra animeringen spring bakåt först, sedan framåt:

Exempel

div {
  width: 100px;
  height: 100px;
  
position: relative;
  background-color: red;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: 
alternate-reverse;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 4s;
  animation-iteration-count: 2;
  animation-direction: alternate-reverse;  
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>The animation-direction property specifies whether an animation should be played forwards, backwards or in alternate cycles. The following example uses the value "alternate-reverse" to make the animation run backwards first, then forwards:</p>

<div></div>

</body>
</html>



Ange hastighetskurvan för animeringen

Egenskapen animation-timing-function anger hastighetskurvan för animation.

Egenskapen animation-timing-function kan ha följande värden:

ease

- Anger en animation med en långsam start, sedan snabb och sedan långsam (detta är standard)

linear

- Anger en animering med samma hastighet från början till slut

ease-in

- Anger en animation med långsam start

ease-out

- Anger en animation med ett långsamt slut

ease-in-out

- Anger en animation med långsam start och slut

cubic-bezier(n,n,n,n)

- Låter dig definiera dina egna värden i en cubic-bezier-funktion

Följande exempel visar några av de olika hastighetskurvorna som kan användas:

Exempel

#div1 {animation-timing-function: linear;}
#div2 
{animation-timing-function: ease;}
#div3 {animation-timing-function: 
ease-in;}
#div4 {animation-timing-function: ease-out;}
#div5 
{animation-timing-function: ease-in-out;}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 50px;
  background-color: red;
  font-weight: bold;
  position: relative;
  animation: mymove 5s;
  animation-fill-mode: forwards;
}

#div1 {animation-timing-function: linear;}
#div2 {animation-timing-function: ease;}
#div3 {animation-timing-function: ease-in;}
#div4 {animation-timing-function: ease-out;}
#div5 {animation-timing-function: ease-in-out;}

@keyframes mymove {
  from {left: 0px;}
  to {left: 300px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>The animation-timing-function property specifies the speed curve of the animation. The following example shows some of the different speed curves that can be used:</p>

<div id="div1">linear</div>
<div id="div2">ease</div>
<div id="div3">ease-in</div>
<div id="div4">ease-out</div>
<div id="div5">ease-in-out</div>

</body>
</html>



Ange fyllningsläget för en animering

CSS-animationer påverkar inte ett element innan den första nyckelbildrutan spelas eller efter att den sista nyckelbildrutan har spelats upp. Egenskapen animation-fill-mode kan åsidosätta detta beteende.

Egenskapen animation-fill-mode anger en stil för målelementet när animeringen inte spelas upp (före den startar, efter det slutar, eller båda).

Egenskapen animation-fill-mode kan ha följande värden:

none

- Standardvärde. Animation kommer inte att tillämpa några stilar på elementet före eller efter att det körs

forwards

- Elementet kommer att behålla stilvärdena som ställs in av den sista nyckelbildrutan (beror på animationsriktning och animation-iteration-antal)

backwards

- Elementet kommer att få stilvärdena som ställs in av den första nyckelbildrutan (beror på animationsriktningen), och behålla detta under animationsfördröjningsperioden

both

- Animationen kommer att följa reglerna för både framåt och bakåt, vilket utökar animeringsegenskaperna i båda riktningarna

Följande exempel låter <div>-elementet behålla stilvärdena från sista nyckelbildrutan när animeringen slutar:

Exempel

 div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;
    animation-fill-mode: forwards;
  }

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;  
  animation-fill-mode: forwards;
}

@keyframes example {
  from {top: 0px;}
  to {top: 200px; background-color: blue;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>Let the div element retain the style values set by the last keyframe when the animation ends:</p>

<div></div>

</body>
</html>


Följande exempel låter <div>-elementet få stilvärdena som anges av första nyckelbildrutan innan animeringen startar (under animationsfördröjningsperioden):

Exempel

 div {
  width: 100px;
  height: 100px;
  
  background: red;
  position: relative;
  
animation-name: example;
  
animation-duration: 3s;
  
animation-delay: 2s;
  animation-fill-mode: backwards;
  }

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;  
  animation-delay: 2s;
  animation-fill-mode: backwards;
}

@keyframes example {
  from {top: 0px; background-color: yellow;}
  to {top: 200px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>Let the div element get the style values set by the first keyframe before the animation starts (during the animation-delay period):</p>

<div></div>

</body>
</html>


Följande exempel låter <div>-elementet få stilvärdena av den första nyckelbildrutan innan animeringen startar, och behåll stilvärdena från den sista nyckelbildrutan när animeringen slutar:

Exempel

 div {
  width: 100px;
  height: 100px;
  background: red;
    position: relative;
  
animation-name: example;
  
animation-duration: 3s;
  
animation-delay: 2s;
  animation-fill-mode: both;
  }

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation-name: example;
  animation-duration: 3s;  
  animation-delay: 2s;
  animation-fill-mode: both;
}

@keyframes example {
  from {top: 0px; background-color: yellow;}
  to {top: 200px; background-color: blue;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>Let the div element get the style values set by the first keyframe before the animation starts, and retain the style values from the last keyframe when the animation ends:</p>

<div></div>

</body>
</html>



Animation Shorthand Property

I exemplet nedan används sex av animeringsegenskaperna:

Exempel

div
{	animation-name: example;
   
animation-duration: 5s;
   
animation-timing-function: linear;
   
animation-delay: 2s;
   
animation-iteration-count: infinite;
   
animation-direction: alternate;
} 

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation-name: example;
  animation-duration: 5s;
  animation-timing-function: linear;
  animation-delay: 2s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes example {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>This example uses six of the animation properties:</p>

<div></div>

</body>
</html>


Samma animationseffekt som ovan kan uppnås genom att använda stenografin animation-egenskap:

Exempel

div
{
    animation: example 5s linear 2s infinite alternate;
}

Prova själv →

<!DOCTYPE html>
<html>
<head>
<style> 
div {
  width: 100px;
  height: 100px;
  background-color: red;
  position: relative;
  animation: myfirst 5s linear 2s infinite alternate;
}

@keyframes myfirst {
  0%   {background-color:red; left:0px; top:0px;}
  25%  {background-color:yellow; left:200px; top:0px;}
  50%  {background-color:blue; left:200px; top:200px;}
  75%  {background-color:green; left:0px; top:200px;}
  100% {background-color:red; left:0px; top:0px;}
}
</style>
</head>
<body>

<h1>CSS Animation</h1>

<p>This example uses the shorthand animation property:</p>

<div></div>

</body>
</html>




CSS-animeringsegenskaper

Följande tabell listar @keyframes-regeln och alla CSS-animeringsegenskaper:

@keyframes

Anger animeringskoden

animation

En förkortningsegenskap för att ställa in alla animeringsegenskaper

animation-delay

Anger en fördröjning för starten av en animering

animation-direction

Anger om en animation ska spelas framåt, bakåt eller i omväxlande cykler

animation-duration

Anger hur lång tid en animering ska ta för att slutföra en cykel

animation-fill-mode

Anger en stil för elementet när animeringen inte spelas (innan den börjar, efter den slutar eller båda)

animation-iteration-count

Anger hur många gånger en animation ska spelas

animation-name

Anger namnet på @keyframes-animeringen

animation-play-state

Anger om animeringen körs eller pausas

animation-timing-function

Anger hastighetskurvan för animeringen