Minakshi
Minakshi

Reputation: 25

Ball not rolling properly CSS

My animation works quite nicely but it seems its slipping and doesn't seem to roll. What could be the problem?

FIDDLE HERE : http://jsfiddle.net/erkoda3m/2/

img {
  height: 150px;
  width: 150px;
  animation: roll 4s linear infinite;
  -webkit-animation: roll 4s linear infinite;
}
@-webkit-keyframes roll {
  0% {
    -webkit-transform: translateX(0px) rotate(0deg);
  }
  50% {
    -webkit-transform: translateX(300px) rotate(360deg);
  }
  100% {
    -webkit-transform: translateX(0px) rotate(0deg);
  }
}
@keyframes roll {
  0% {
    transform: translateX(0px) rotate(0deg);
  }
  50% {
    transform: translateX(300px) rotate(360deg);
  }
  100% {
    transform: translateX(0px) rotate(0deg);
  }
}
<img src="http://i.imgur.com/5NvOwB5.png">

Upvotes: 0

Views: 104

Answers (1)

The Pragmatick
The Pragmatick

Reputation: 5477

The diameter is 150px, so circumference will be 150π = 471.24px. So if the rotation angle is 360°, translateX value will be circumference of the ball, or 471.24px :

img {
  height: 150px;
  width: 150px;
  animation: roll 4s linear infinite;
  -webkit-animation: roll 4s linear infinite;
}
@-webkit-keyframes roll {
  0% {
    -webkit-transform: translateX(0px) rotate(0deg);
  }
  50% {
    -webkit-transform: translateX(471.24px) rotate(360deg);
  }
  100% {
    -webkit-transform: translateX(0px) rotate(0deg);
  }
}
@keyframes roll {
  0% {
    transform: translateX(0px) rotate(0deg);
  }
  50% {
    transform: translateX(471.24px) rotate(360deg);
  }
  100% {
    transform: translateX(0px) rotate(0deg);
  }
}
<img src="http://i.imgur.com/5NvOwB5.png">

If you want to keep translateX constant and change rotate value, instead, you can calculate the angle by (400/471.24)*360 = 305.57deg

img {
  height: 150px;
  width: 150px;
  animation: roll 4s linear infinite;
  -webkit-animation: roll 4s linear infinite;
}
@-webkit-keyframes roll {
  0% {
    -webkit-transform: translateX(0px) rotate(0deg);
  }
  50% {
    -webkit-transform: translateX(400px) rotate(305.57deg);
  }
  100% {
    -webkit-transform: translateX(0px) rotate(0deg);
  }
}
@keyframes roll {
  0% {
    transform: translateX(0px) rotate(0deg);
  }
  50% {
    transform: translateX(400px) rotate(305.57deg);
  }
  100% {
    transform: translateX(0px) rotate(0deg);
  }
}
<img src="http://i.imgur.com/5NvOwB5.png">

Upvotes: 7

Related Questions