Skip to content

Commit 02abd60

Browse files
committed
JavaScript DOM Manipulation and Projects
1 parent 3d5317d commit 02abd60

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed
Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Animations in DOM</title>
7+
<style>
8+
#container {
9+
width: 500px;
10+
height: 500px;
11+
background: rgb(118, 252, 252);
12+
position: relative;
13+
border-radius: 10px;
14+
border: 1px solid black;
15+
margin: auto;
16+
}
17+
18+
#inside {
19+
width: 100px;
20+
height: 100px;
21+
background: rgb(243, 86, 86);
22+
position: absolute;
23+
border-radius: 10px;
24+
}
25+
26+
.div {
27+
display: flex;
28+
align-items: center;
29+
justify-content: center;
30+
gap: 20px;
31+
margin-top: 10px;
32+
}
33+
34+
#button {
35+
width: 200px;
36+
border-radius: 10px;
37+
border: none;
38+
background: rgb(239, 97, 97);
39+
cursor: pointer;
40+
}
41+
</style>
42+
</head>
43+
<body>
44+
<div id="container">
45+
<div id="inside"></div>
46+
</div>
47+
<div class="div">
48+
<button id="button" onclick="moveDiagonally()"><h4>Click to move the box digonally</h4></button>
49+
<button id="button" onclick="moveAllFourSides()"><h4>Click to move the box to the each side of the square</h4></button>
50+
<button id="resetButton" onclick="reset()"><h3>Click to reset</h3></button>
51+
</div>
52+
<script src="./index.js"></script>
53+
</body>
54+
</html>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// function myMove () {
2+
// let pos = 0
3+
// const element = document.getElementById('inside')
4+
5+
// const id = setInterval(frame, 10)
6+
7+
// function frame () {
8+
// if (pos == 400) {
9+
// clearInterval(id)
10+
// }
11+
// pos++
12+
// element.style.top = pos + 'px'
13+
// element.style.left = pos++ + 'px'
14+
// }
15+
// }
16+
17+
// More optimized code
18+
function moveDiagonally () {
19+
let pos = 0
20+
const element = document.getElementById('inside')
21+
const resetButton = document.getElementById('resetButton')
22+
23+
const id = setInterval(() => {
24+
25+
resetButton.addEventListener('click', reset)
26+
27+
if (pos >= 400) {
28+
clearInterval(id)
29+
} else {
30+
pos++
31+
element.style.top = pos + 'px'
32+
element.style.left = pos++ + 'px'
33+
}
34+
}, 10)
35+
36+
function reset () {
37+
clearInterval(id) // Stop the animation
38+
pos = 0
39+
direction = 0
40+
element.style.top = '0px'
41+
element.style.left = '0px'
42+
}
43+
44+
r
45+
}
46+
47+
function moveAllFourSides () {
48+
let pos = 0
49+
let direction = 0
50+
const element = document.getElementById('inside')
51+
const resetButton = document.getElementById('resetButton')
52+
53+
const id = setInterval(frame, 10)
54+
55+
resetButton.addEventListener('click', reset)
56+
57+
switch (direction) {
58+
case 0: // Move from top-left to top-right
59+
if (pos >= 400) {
60+
direction = 1
61+
pos = 0
62+
} else {
63+
pos++
64+
element.style.left = pos + 'px'
65+
}
66+
break
67+
case 1: // Move from top-right to bottom-right
68+
if (pos >= 400) {
69+
direction = 2
70+
pos = 0
71+
} else {
72+
pos++
73+
element.style.top = pos + 'px'
74+
}
75+
break
76+
case 2: // Move from bottom-right to bottom-left
77+
if (pos >= 400) {
78+
direction = 3
79+
pos = 0
80+
} else {
81+
pos++
82+
element.style.left = (400 - pos) + 'px'
83+
}
84+
break
85+
case 3: // Move from bottom-left to top-left
86+
if (pos >= 400) {
87+
clearInterval(id)
88+
reset()
89+
} else {
90+
pos++
91+
element.style.top = (400 - pos) + 'px'
92+
}
93+
break
94+
}
95+
}
96+
97+
function reset () {
98+
clearInterval(id) // Stop the animation
99+
pos = 0
100+
direction = 0
101+
element.style.top = '0px'
102+
element.style.left = '0px'
103+
}

0 commit comments

Comments
 (0)