|
| 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) |
0 commit comments