w3resource

CSS Properties: How to play the animation has the same speed from start to end?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<title>How to play the animation has the same speed from start to end</title><!-- Set the title of the HTML document -->
<style> /* CSS style start*/
div {
width: 80px; /* Set the width of the div */
height: 120px; /* Set the height of the div */
background: #CC00CC; /* Set the background color of the div */
position: relative; /* Set the position of the div to relative */
-webkit-animation: move 8s infinite;  /* Chrome, Safari, Opera */ /* Apply animation named "move" with a duration of 8 seconds and infinite iterations for WebKit browsers */
-webkit-animation-timing-function: linear;  /* Chrome, Safari, Opera */ /* Specify linear timing function for animation in WebKit browsers */
animation: move 8s infinite; /* Apply animation named "move" with a duration of 8 seconds and infinite iterations */
animation-timing-function: linear; /* Specify linear timing function for animation */
}
/* Chrome, Safari, Opera */
@-webkit-keyframes move {
from {left: 0px;} /* Define initial position */
to {left: 200px;} /* Define final position */
}

@keyframes move {
    from {left: 0px;} /* Define initial position */
    to {left: 200px;} /* Define final position */
}
</style>
<body>

<p>w3resource</p>
<div></div><!-- Create a div element -->
<p>Tutorial</p>
</body>
</html>

Explanation:

  • This HTML document contains a paragraph element with the text "w3resource", a div element, and another paragraph element with the text "Tutorial".
  • CSS is used to style the div element, setting its width, height, background color, and position.
  • An animation named "move" is applied to the div element using the animation property, specifying the name of the animation, its duration (8 seconds), and infinite iterations.
  • The animation-timing-function property is used to specify a linear timing function, ensuring that the animation progresses at a constant speed from start to end.
  • Keyframes are defined to animate the div from the initial position to 200px to the right over a duration of 8 seconds.

Live Demo:

See the Pen animation-timing-function-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes Yes No

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.