从中分割动画
.rocket
选择器到另一个类-随便调用它,然后将该类添加到元素中。单击,移除该类并添加您现在添加的类
这样地
function flyRocket() {
let animatedElement = document.querySelector('.rocket');
animatedElement.classList.remove('up-down-animation');
moveRocket();
}
function moveRocket() {
let movingElement = document.querySelector('.rocket');
movingElement.classList.add('move-up-animation');
}
document.body.addEventListener('click', flyRocket);
#Rocket {
height: 100px;
width: 50px;
}
.rocket {
position: relative;
}
.up-down-animation {
animation: animate 0.5s ease infinite;
}
@keyframes animate {
0%, 100% {
transform: translateY(-4px);
}
50% {
transform: translateY(4px);
}
}
.rocket::before {
content: '';
position: absolute;
bottom: -40px;
left: 50%;
transform: translateX(-50%);
width: 10px;
height: 50px;
background: linear-gradient(#00d0ff, transparent);
}
.rocket::after {
content: '';
position: absolute;
top: 5;
left: 50%;
transform: translateX(-50%);
width: 10px;
height: 50px;
background: linear-gradient(#00d0ff, transparent);
filter: blur(20px);
}
.move-up-animation {
animation: moveUpAnimation 2s ease forwards;
}
@keyframes moveUpAnimation {
0% {
transform: translateY(0);
}
100% {
transform: translateY(-50px);
}
}
<div class='scene'>
<div class='rocket up-down-animation'>
<img id='Rocket' src='rocket.png'>
</div>
</div>