Вот упрощенная версия моего кода:
HTML:
Код: Выделить всё
Код: Выделить всё
#animatedContainer {
width: 100%;
height: 100vh;
overflow: hidden;
position: relative;
}
.movingBox {
width: 50px;
height: 50px;
background-color: #3498db;
position: absolute;
animation: moveBox 5s infinite alternate;
}
@keyframes moveBox {
0% {
left: 0;
top: 0;
transform: scale(1);
}
50% {
left: 50%;
top: 50%;
transform: scale(1.5);
}
100% {
left: 100%;
top: 100%;
transform: scale(1);
}
}
Код: Выделить всё
document.addEventListener("DOMContentLoaded", function() {
const container = document.getElementById("animatedContainer");
const box = document.querySelector(".movingBox");
container.addEventListener("mouseover", () => {
box.style.animationPlayState = 'paused';
});
container.addEventListener("mouseout", () => {
box.style.animationPlayState = 'running';
});
// Additional JavaScript to control animation with more complex logic
let animationRunning = true;
function toggleAnimation() {
if (animationRunning) {
box.style.animation = 'none';
animationRunning = false;
} else {
box.style.animation = 'moveBox 5s infinite alternate';
animationRunning = true;
}
}
document.addEventListener("click", toggleAnimation);
});
Подробнее здесь: https://stackoverflow.com/questions/786 ... -animation