console
const container = document.querySelector('.container')
const ring = document.querySelector('.ring')
let play = true;
let goId;
let backId;
function go(){
container.style.transform="rotateY(0deg)";
ring.style.animationDirection = 'normal';
let left;
if(!container.style.left){
left = 0;
}else{
left = parseInt(container.style.left);
}
left+=1;
if(left>500){
backId=window.requestAnimationFrame(back)
}else{
goId=window.requestAnimationFrame(go)
}
container.style.left = left + 'px';
}
function back(){
container.style.transform="rotateY(180deg)";
ring.style.animationDirection = 'reverse';
let left = parseInt(container.style.left);
left -= 1;
if(left<=0){
goId=window.requestAnimationFrame(go)
}else{
backId=window.requestAnimationFrame(back)
}
container.style.left = left + 'px';
}
goId=window.requestAnimationFrame(go)
document.addEventListener('click', ()=>{
play = !play;
if(play){
go()
}else{
window.cancelAnimationFrame(goId);
window.cancelAnimationFrame(backId);
}
})
<div class="container">
<div class="people">
<div class="head"></div>
<div class="body"></div>
<div class="foot">
<div class="left-foot"></div>
<div class="right-foot"></div>
</div>
</div>
<div class="joy">
<div class="handle"></div>
<div class="ring"></div>
</div>
</div>
.container{
widows: 100px;
height: 200px;
position: absolute;
.people{
width: 100px;
height: 300px;
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
.head{
width: 10px;
height: 30px;
background-color: #000;
border-radius: 15px;
position: relative;
&::before{
content: '';
position: absolute;
width: 10px;
height: 3px;
top: 10px;
right: -10px;
border-radius: 3px;
background: #000;
}
}
.body{
width: 40px;
height: 60px;
background-color: #000;
border-radius: 25px;
}
.foot{
display: flex;
justify-content: space-between;
gap: 5px;
margin-top: -10px;
.left-foot, .right-foot{
width: 10px;
height: 80px;
background-color: #000;
border-radius: 40px;
animation: walk 1s linear infinite;
transform-origin:50% 0;
}
.right-foot{
animation-direction: reverse;
}
}
.handle{
width: 5px;
height: 200px;
background-color: #000;
transform: rotate(-45deg);
border-radius: 5px;
}
}
.joy{
position: absolute;
width: 50px;
height: 200px;
left: 100px;
top: 30px;
.handle{
width: 5px;
height: 150px;
background-color: #000;
transform: rotate(-45deg);
}
.ring{
width: 50px;
height: 50px;
border-radius: 50%;
border: 5px dashed #000;
position: absolute;
right: -35px;
bottom: 45px;
animation: ring 3s linear infinite;
}
}
}
@keyframes walk{
from{
transform: rotate(-25deg)
}
to{
transform: rotate(25deg)
}
}
@keyframes ring{
from {
transform: rotate(0)
}
to{
transform: rotate(360deg)
}
}