Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate an element on a sine path

sine wave or curve path for element animation I neeed to move an element on sine wave or path as shown in above image. I made something. But it is not working as I want.

img {
  position:absolute;
  animation: roam infinite;
  animation-duration: 15s;
}

@keyframes roam {
  0% { left: 50px; top:100px }
  10%{ left:100px; top:100px }
  20%{ left:200px; top:200px }
  30%{ left:300px; top:50px }
  40%{ left:400px; top:200px }
	
}
<img src="https://developer.chrome.com/extensions/examples/api/idle/idle_simple/sample-128.png">
like image 238
Sagar Kodte Avatar asked Apr 14 '16 09:04

Sagar Kodte


People also ask

What is linear animation?

Linear approach It's an animation starting slowly and gradually transforming into a bit faster animation. This is not good for animating 360° as it would move slower in the beginning and faster in the end of its full revolution. Linear type of animation is better for our purpose. It's is a movement at constant speed.

What is animation-timing-function cubic Bezier?

CSS cubic-bezier() FunctionP0 is (0, 0) and represents the initial time and the initial state, P3 is (1, 1) and represents the final time and the final state. The cubic-bezier() function can be used with the animation-timing-function property and the transition-timing-function property.

What is animation-timing-function linear?

The animation-timing-function specifies the speed curve of an animation. The speed curve defines the TIME an animation uses to change from one set of CSS styles to another. The speed curve is used to make the changes smoothly.


1 Answers

You can move an element on a sine path with 2 CSS keyframe animations. The point is to translate left right with a linear timing function and up/down with an ease-in-out timing function.

This requires to translate the container and move the element up and down with 2 different keyframe animations. Here is an example :

div{
  position:absolute;
  left:0;
  width:10%;
  animation: translate 15s infinite linear;
}
img {
  position:absolute;
  animation: upDown 1.5s alternate infinite ease-in-out;
  width:100%;
}

@keyframes upDown {
  to { transform: translatey(100px);}
}
@keyframes translate {
  to { transform: translatex(900%);}
}
<div>
  <img src="http://i.imgur.com/QdWJXta.png">
</div>

Note that this example doesn't contain vendor prefixes. For more info, see canIuse for:

  • CSS keyframe animations
  • CSS 2D transforms
like image 135
web-tiki Avatar answered Sep 25 '22 00:09

web-tiki