Web Design 4th Assignment
Web Design 4th Assignment
Assignment#4
<!DOCTYPE html>
<html>
<style>
#container {
width: 490px;
height: 490px;
position: relative;
background: black;
}
#animate {
width: 120px;
height: 120px;
position: absolute;
background-image: url('nature.jpg');
background-size: cover;
}
</style>
<body>
<div id="container">
<div id="animate"></div>
</div>
<script>
let direction = 1; // 1 for moving right, -1 for moving left
let pos = 0; // initial position
let id = null;
function myMove() {
const elem = document.getElementById("animate");
if (id === null) { // Check if animation is already running
id = setInterval(frame, 5);
}
}
function frame() {
if (pos == 350) {
direction = -1; // Change direction to move left
} else if (pos === 0) {
direction = 1; // Change direction to move right
clearInterval(id); // Stop the animation
id = null; // Reset the interval ID
}
</body>
</html>