• We just launched and are currently in beta. Join us as we build and grow the community.

Animation Duration in CSS

deniz777

Endpoint Security Evaluator
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
71
Likes
140
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial, we are going to learn about animation-duration.

So, what is animation-duration?

The animation-duration property specifies how many seconds or milliseconds an animation takes to complete one cycle.

This property has a syntax:
animation-duration: time | initial | inherit ;

Property Values
time - it specifies the length of time an animation takes to finish. The default value is equal to 0s.
initial - it specifies that the value of the property should be set to the default value.

inherit - it specifies that the value of the property should be inherited from the parent element.

Example

This example shows the use of the animation-duration property in a three (3) different value of time.

CSS Source Code

  1. <style type=

    "text/css"

    >

  2. div.animation

    {
  3. width

    :

    60px

    ;
  4. height

    :

    50px

    ;
  5. color

    :

    white

    ;
  6. padding

    :

    2px

    ;
  7. background

    :

    blue

    ;
  8. position

    :

    relative

    ;
  9. }

  10. div.animation1

    {
  11. width

    :

    60px

    ;
  12. height

    :

    50px

    ;
  13. color

    :

    white

    ;
  14. padding

    :

    2px

    ;
  15. background

    :

    red

    ;
  16. position

    :

    relative

    ;
  17. }

  18. div.animation2

    {
  19. width

    :

    60px

    ;
  20. height

    :

    50px

    ;
  21. color

    :

    white

    ;
  22. padding

    :

    2px

    ;
  23. background

    :

    green

    ;
  24. position

    :

    relative

    ;
  25. }

  26. #test1

    {
  27. animation-name

    :

    boxmove;
  28. animation-duration

    :

    5s

    ;
  29. animation-iteration-count

    :

    infinite

    ;
  30. /* Firefox */
  31. -moz-animation-name:

    boxmove;
  32. -moz-animation-duration:

    5s

    ;
  33. -moz-animation-iteration-count:

    infinite

    ;
  34. /* Safari and Google Chrome */
  35. -webkit-animation-name:

    boxmove;
  36. -webkit-animation-duration:

    5s

    ;
  37. -webkit-animation-iteration-count:

    infinite

    ;
  38. }

  39. #test2

    {
  40. animation-name

    :

    boxmove;
  41. animation-duration

    :

    10s

    ;
  42. animation-iteration-count

    :

    infinite

    ;
  43. /* Firefox */
  44. -moz-animation-name:

    boxmove;
  45. -moz-animation-duration:

    10s

    ;
  46. -moz-animation-iteration-count:

    infinite

    ;
  47. /* Safari and Google Chrome */
  48. -webkit-animation-name:

    boxmove;
  49. -webkit-animation-duration:

    10s

    ;
  50. -webkit-animation-iteration-count:

    infinite

    ;
  51. }

  52. #test3

    {
  53. animation-name

    :

    boxmove;
  54. animation-duration

    :

    15s

    ;
  55. animation-iteration-count

    :

    infinite

    ;
  56. /* Firefox */
  57. -moz-animation-name:

    boxmove;
  58. -moz-animation-duration:

    15s

    ;
  59. -moz-animation-iteration-count:

    infinite

    ;
  60. /* Safari and Google Chrome */
  61. -webkit-animation-name:

    boxmove;
  62. -webkit-animation-duration:

    15s

    ;
  63. -webkit-animation-iteration-count:

    infinite

    ;
  64. }

  65. @keyframes

    boxmove
  66. {
  67. from {

    left

    :

    0px

    ;

    }
  68. to {

    left

    :

    210px

    ;

    }
  69. }

  70. @-moz-keyframes

    boxmove /* Firefox */
  71. {
  72. from {

    left

    :

    0px

    ;

    }
  73. to {

    left

    :

    210px

    ;

    }
  74. }

  75. @-webkit-keyframes

    boxmove /* Safari and Google Chrome */
  76. {
  77. from {

    left

    :

    0px

    ;

    }
  78. to {

    left

    :

    210px

    ;

    }
  79. }

  80. </style>

The complete code.
  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    CSS3 animation-duration</

    title

    >

  5. <style

    type

    =

    "text/css"

    >

  6. div.animation {
  7. width:60px;
  8. height:50px;
  9. color:white;
  10. padding:2px;
  11. background:blue;
  12. position:relative;
  13. }

  14. div.animation1 {
  15. width:60px;
  16. height:50px;
  17. color:white;
  18. padding:2px;
  19. background:red;
  20. position:relative;
  21. }

  22. div.animation2 {
  23. width:60px;
  24. height:50px;
  25. color:white;
  26. padding:2px;
  27. background:green;
  28. position:relative;
  29. }

  30. #test1 {
  31. animation-name:boxmove;
  32. animation-duration:5s;
  33. animation-iteration-count:infinite;
  34. /* Firefox */
  35. -moz-animation-name:boxmove;
  36. -moz-animation-duration:5s;
  37. -moz-animation-iteration-count:infinite;
  38. /* Safari and Google Chrome */
  39. -webkit-animation-name:boxmove;
  40. -webkit-animation-duration:5s;
  41. -webkit-animation-iteration-count:infinite;
  42. }

  43. #test2 {
  44. animation-name:boxmove;
  45. animation-duration:10s;
  46. animation-iteration-count:infinite;
  47. /* Firefox */
  48. -moz-animation-name:boxmove;
  49. -moz-animation-duration:10s;
  50. -moz-animation-iteration-count:infinite;
  51. /* Safari and Google Chrome */
  52. -webkit-animation-name:boxmove;
  53. -webkit-animation-duration:10s;
  54. -webkit-animation-iteration-count:infinite;
  55. }

  56. #test3 {
  57. animation-name:boxmove;
  58. animation-duration:15s;
  59. animation-iteration-count:infinite;
  60. /* Firefox */
  61. -moz-animation-name:boxmove;
  62. -moz-animation-duration:15s;
  63. -moz-animation-iteration-count:infinite;
  64. /* Safari and Google Chrome */
  65. -webkit-animation-name:boxmove;
  66. -webkit-animation-duration:15s;
  67. -webkit-animation-iteration-count:infinite;
  68. }

  69. @keyframes boxmove
  70. {
  71. from {left:0px;}
  72. to {left:210px;}
  73. }

  74. @-moz-keyframes boxmove /* Firefox */
  75. {
  76. from {left:0px;}
  77. to {left:210px;}
  78. }

  79. @-webkit-keyframes boxmove /* Safari and Google Chrome */
  80. {
  81. from {left:0px;}
  82. to {left:210px;}
  83. }

  84. </

    style

    >

  85. </

    head

    >
  86. <body

    >

  87. <div

    id

    =

    "test1"

    class

    =

    "animation"

    >

    5s duration</

    div

    >
  88. <br

    /

    >
  89. <div

    id

    =

    "test2"

    class

    =

    "animation1"

    >

    10s duration</

    div

    >
  90. <br

    /

    >
  91. <div

    id

    =

    "test3"

    class

    =

    "animation2"

    >

    15s duration</

    div

    >

  92. </

    body

    >
  93. </

    html

    >

This is the result of the codes above:
result_3.png


Hope that this tutorial will help you a lot.

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top