Race
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Racing Objects Animation</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
padding-top: 50px;
}
.race-track {
position: relative;
width: 100%;
max-width: 800px;
height: 150px;
margin: 20px auto;
background-color: #ddd;
border: 2px solid #555;
}
.car {
position: absolute;
top: 20px;
left: 0;
width: 50px;
height: 50px;
border-radius: 10px;
text-align: center;
line-height: 50px;
color: #fff;
font-weight: bold;
}
.car1 {
background-color: red;
}
.car2 {
background-color: blue;
top: 80px;
}
.buttons {
margin-top: 20px;
}
button {
padding: 10px 20px;
font-size: 16px;
margin: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Racing Objects Animation</h1>
<div class="race-track">
<div class="car car1">A</div>
<div class="car car2">B</div>
</div>
<div class="buttons">
<button id="startBtn">Start Race</button>
<button id="resetBtn">Reset</button>
</div>
<script>
$(document).ready(function () {
$('#startBtn').click(function () {
// Reset cars to starting line
$('.car').stop().css('left', '0');
// Random durations for racing effect
let raceDuration1 = Math.floor(Math.random() * 3000) + 3000;
let raceDuration2 = Math.floor(Math.random() * 3000) + 3000;
// Animate cars
$('.car1').animate({ left: '750px' }, raceDuration1);
$('.car2').animate({ left: '750px' }, raceDuration2);
});
$('#resetBtn').click(function () {
// Stop current animations and reset positions
$('.car').stop().css('left', '0');
});
});
</script>
</body>
</html>
Comments
Post a Comment