I have a <div class="sphere" />
that I want to move in the shape of infinity symbol. So I thought the answer should be to fix transform-origin
to left
, from 0 to 50%, make it do a full rotation, then change transform-origin
to right
, and from 50.01% to 100% make it do a full rotation again and reset tranform origin like so
.sphere {
position: absolute;
width: var(--sphere-dim);
height: var(--sphere-dim);
background: rgb(183, 162, 159);
border-radius: 50%;
top: 40%;
left: 40%;
transform-origin: left;
/* transition: all 1.5s; */
/* animation-fill-mode: forwards; */
animation: infinityWobble 5s infinite;
}
@keyframes infinityWobble {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(360deg);
}
50.01% {
transform: rotate(0deg);
transform-origin: right;
}
99% {
transform: rotate(360deg);
}
100% {
transform-origin: left;
}
}
https://codepen.io/meherwan-singh-gill/pen/ExOKxmv
^ this is the result
2
Answers
As I mentioned the
rotate
animation will be not visible because the element you are rotating is a circle. So in this case you either need to play withtransformX
andtransformY
, or another approach will be changing thetop
andleft
position of the element. Here is an example. Hope it helps a bit 🙂I have a detailed article on how to create such an animation (and more): https://css-tricks.com/advanced-css-animation-using-cubic-bezier/
Here is the first code from the article:
Another version with transform for better performance