Skip to content

Commit df94151

Browse files
committed
Section 13: Another Fun DOM Example
1 parent b1a706e commit df94151

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

File renamed without changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// ******************************
2+
// Another Fun DOM Example ******************************
3+
// Example 1
4+
/*
5+
// Go to https://google.com/
6+
// Create a snippet
7+
8+
// 1. Go to DevTools in Google Chrome
9+
// 2. Go to Sources Tab
10+
// 3. Go to Snippets Tab
11+
// 4. New snippet
12+
// Start writing code in the new snippet
13+
14+
console.log(2+2)
15+
16+
// Ctrl + Enter: To run the code in the new snippet
17+
18+
const myImg = document.createElement('img')
19+
myImg.src =
20+
'https://cloudfront-us-east-1.images.arcpublishing.com/metroworldnews/55QHEOQRQBEPTF5LRJK57MDEP4.jpg'
21+
22+
myImg.style.width = '200px'
23+
document.body.append(myImg)
24+
myImg.style.transition = 'all 2s'
25+
26+
// Moving the image
27+
// myImg.style.transform = 'translate(300px, 200px)'
28+
29+
// Getting the windo width of the client
30+
// document.body.clientWidth
31+
32+
// Moving the image randomly
33+
setInterval(() => {
34+
const x = Math.floor(document.body.clientWidth * Math.random())
35+
const y = Math.floor(document.body.clientHeight * Math.random())
36+
myImg.style.transform = `translate(${x}px,${y}px)`
37+
}, 2000)
38+
*/
39+
40+
// ####################
41+
// Move everything
42+
// Example 2
43+
// On the same code snippet of Example 1 test this code
44+
45+
const allElements = document.body.children
46+
47+
setInterval(() => {
48+
for (element of allElements) {
49+
const rotation = Math.floor(Math.random() * 360)
50+
51+
const x = Math.floor(document.body.clientWidth * Math.random())
52+
const y = Math.floor(document.body.clientHeight * Math.random())
53+
54+
// element.style.transform = `translate(${x}px,${y}px)`
55+
element.style.transform = `translate(${x}px,${y}px) rotate(${rotation}deg)`
56+
}
57+
}, 2000)

13-JS-In-the-Browser-DOM-Manipulation/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ The Document object is our entry point in the world of the DOM It contains repre
5757
### SELECTING
5858
- getElementById
5959
- getElementsByTagName
60-
- getElementsByClassName
60+
- getElementsByClassName
61+
62+
63+
## 4. Another Fun DOM Example

13-JS-In-the-Browser-DOM-Manipulation/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ <h1 class="header">My WebPage</h1>
3535
src="https://images.unsplash.com/photo-1547146092-983100a60066?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80"
3636
alt="alt tag"
3737
/>
38-
<script src="app.js"></script>
38+
<!-- <script src="app.js"></script> -->
3939
</body>
4040
</html>

0 commit comments

Comments
 (0)