"Mesmerizing Animated Ball Reels | HTML, CSS, JavaScript Animation Tutorial"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animated Ball</title>
<style>
body {
margin: 0;
overflow: hidden;
}
#ball {
width: 50px;
height: 50px;
background-color: #3498db;
border-radius: 50%;
position: absolute;
}
</style>
</head>
<body>
<div id="ball"></div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const ball = document.getElementById('ball');
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const ballSize = 50;
let x = screenWidth / 2 - ballSize / 2;
let y = screenHeight / 2 - ballSize / 2;
let speedX = 5;
let speedY = 3;
function animateBall() {
x += speedX;
y += speedY;
if (x + ballSize > screenWidth || x < 0) {
speedX = -speedX;
}
if (y + ballSize > screenHeight || y < 0) {
speedY = -speedY;
}
ball.style.left = x + 'px';
ball.style.top = y + 'px';
requestAnimationFrame(animateBall);
}
animateBall();
});
</script>
</body>
</html>
Comments
Post a Comment