2

I have a red box as the element to animate.

Here is a simple representation of how I want to animate the red box.

enter image description here

Here is a try but as you see the anchor point of the movement is at the left of the box no the center as I wish to be:

.yo-yo {
  display: block;
  position: absolute;
  height: 50px;
  width: 200px;
  background: red; 
  transform-origin: 50% 50%;
  animation: yo-yo 0.5s infinite alternate;  /* Animation speed and type */
}

/* Animation beginning and ending */
@keyframes yo-yo {
  from {  left: 0 }
  to {  left: 20px }
}
<span class="yo-yo"></span>

Here is the script tag of that specific version of TweenMax:

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js" integrity="sha512-DkPsH9LzNzZaZjCszwKrooKwgjArJDiEjA5tTgr3YX4E6TYv93ICS8T41yFHJnnSmGpnf0Mvb5NhScYbwvhn2w==" crossorigin="anonymous"></script>
Sara Ree
  • 3,417
  • 12
  • 48
  • This has been answered [on the GreenSock forums](https://greensock.com/forums/topic/25217-animate-an-element-to-left-and-right-like-a-yoyo-smoothly/?tab=comments#comment-121654). – Zach Saucier Aug 19 '20 at 20:54

3 Answers3

0

You need to use the progress instead of just simply declaring a from and to in the keyframes rule

Something like this would suffice. If you want a repeating animation then just add a infinite before linear

html,
body {
  padding: 0;
  margin: 0;
  outline: none;
  border: 0;
}

.yo-yo {
  display: block;
  position: absolute;
  height: 50px;
  width: 200px;
  background: red;
  transform-origin: 50% 50%;
  animation: yo-yo 2s linear;
  /* Animation speed and type */
  animation-timing-function: ease-in-out;
}


/* Animation beginning and ending */

@keyframes yo-yo {
  0% {
    left: 0px;
  }
  25% {
    left: -100px;
  }
  50% {
    left: 0px;
  }
  75% {
    left: 100px;
  }
  100% {
    left: 0px;
  }
}
<span class="yo-yo"></span>
Dev Man
  • 2,114
  • 3
  • 23
  • 38
0

Your making it too complicated. Just use the percentages in @keyframes, and use transform: translateX(number) like so:

@keyframes yo-yo {
  0% {transform: translateX(0); }
  25% { transform: translateX(-20px); }
  75% { transform: translateX(20px);  }
  100% { transform: translateX(0px);  }
}

You should try to use transform instead of giving positions. For a better understanding, checkout this link CSS translation vs changing absolute positioning values.

Amin Darian
  • 177
  • 2
  • 11
-1

You can use animation with infinite normal linear. If you remove infinite from animation then it will stop after one iteration. Your animation is divided into 5 parts. 0, all the way right, 0, all the way left, 0. So start changing the left position from 0%, 25%, 50%, 75% and 100%.

.yoyo-container{
  background-color: #DEEBF7;
  padding:12px;
  position: relative;
  width: 200px;
  height: 16px;
  
  
  margin: 0 auto;
}

.yo-yo {
  display: block;
  position: absolute;
  height: 16px;
  width: 200px;
  background: red; 
  transform-origin: 50% 50%;
  animation: yo-yo 2s infinite normal linear;  /* Animation speed and type */
}

/* Animation beginning and ending */
@keyframes yo-yo {
   0% {transform: translateX(0); }
  25% { transform: translateX(50%); }
  50% { transform: translateX(0);  }
  75% { transform: translateX(-50%);  }
  100% { transform: translateX(0);  }
}
<div class="yoyo-container">
<span class="yo-yo"></span>
</div>
Nimitt Shah
  • 4,477
  • 2
  • 10
  • 21