-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-example.js
67 lines (57 loc) · 2.03 KB
/
script-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Practice: Play with event listeners
* - Use an event listener and CSS either inline or through an added class to draw a highlight around the entire grid when you hover over it with your mouse.
* - Add an event listener to each grid cell to highlight that cell when you hover your mouse over it.
* - Add an event listener to each grid cell to change its background color when it is clicked.
* - Add an event listener to a specific key on the keyboard to change the background color of the whole page - from dark to light and back again.
*/
const gridContainer = document.querySelector(".grid");
gridContainer.addEventListener("mouseenter", () => {
gridContainer.style.outline = "6px solid red";
});
gridContainer.addEventListener("mouseleave", () => {
gridContainer.style.outline = "";
});
/**
* Function to generate random hex color
*/
const randColor = () => {
let hexColor = Math.floor(Math.random() * 16777215).toString(16);
return hexColor;
};
// Get all cells
const gridCells = document.querySelectorAll(".cell");
// For each cell, add eventlisteners aplenty
gridCells.forEach((cell) => {
// Set outline when cell is hovered
cell.addEventListener("mouseenter", (e) => {
console.log(e);
cell.style.outline = "2px solid red";
});
// Remove outline when cell is exited
cell.addEventListener("mouseleave", () => {
cell.style.outline = "";
});
// Set/remove random background color on click
cell.addEventListener("click", () => {
if (cell.style.backgroundColor) {
cell.style.backgroundColor = "";
} else {
cell.style.backgroundColor = `#${randColor()}`;
}
});
});
/**
* Set page background using the "d" key on the keyboard
*/
const body = document.body;
body.addEventListener("keydown", (event) => {
// event.code holds the current key pressed:
console.log(event.code);
// Test for KeyD (the "d" key)
if (event.code === "KeyD") {
body.style.backgroundColor === ""
? (body.style.backgroundColor = "hsl(201, 34%, 13%)")
: (body.style.backgroundColor = "");
}
});