According to your updated request (that the object should always remain vertical while rotating), I have made adjustments to my previous code.
There might be another method, but this is the only one I could come up with at the moment. Here, I have placed our original 'ball' element inside another div. Now, the outer div handles the standard pendulum animation. However, the inner object also undergoes a counter-rotating animation to maintain its vertical alignment throughout the movement.
It's important to note that the inner object has its transform-origin set as default, which is center center
, allowing it to rotate around its own axis exclusively.
#container
{
background-color: #777;
border-radius: 50%;
height: 20px;
margin: 0 auto;
margin-top: 150px;
position: relative;
width: 20px;
}
#ball
{
animation: swing 1s ease 0s infinite;
background-color: green;
height: 50px;
left: -15px;
position: absolute;
top: -150px;
transform-origin: center 150px;
width: 50px;
}
@keyframes swing{
0%{transform: rotate(-22.5deg);}
50%{transform: rotate(22.5deg);}
100%{transform: rotate(-22.5deg);}
}
#main-content
{
animation: innerswing 1s ease 0s infinite;
background-color: red;
display: block;
height: 100%;
width: 100%;
}
@keyframes innerswing{
0%{transform: rotate(22.5deg);}
50%{transform: rotate(-22.5deg);}
100%{transform: rotate(22.5deg);}
}
<div id="container">
<div id="ball">
<div id="main-content"></div>
</div>
</div>