-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (67 loc) · 2.44 KB
/
script.js
File metadata and controls
79 lines (67 loc) · 2.44 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
// selecting div with class container
const container = document.querySelector(".container");
// change grid button
const gridButton = document.querySelector('.btn-change-grid');
// change color button
const colorButton = document.querySelector('.btn-change-color');
const getColorButton = document.querySelector('.btn-color');
// setting the grid size
let gridSize;
// setting custom color background
let customBackground = ["808080"];
// set if the user setting a custom color
let customColor = "default";
// selecting random color button
let randomColorButton = document.querySelector('.random-color-button');
// getting head element
let headElement = document.getElementsByTagName('head')[0];
// function for changing grid
function changeGrid() {
gridSize = prompt('How many grid do you want?');
if(gridSize > 100 || gridSize < 0) {
alert("Grid size can choose between 0 and 100");
} else {
container.innerHTML = '';
for (let i = 0; i < gridSize; i++) {
const divContainer = document.createElement('div')
divContainer.classList.add('div-container');
container.appendChild(divContainer);
for (let j = 0; j < gridSize; j++) {
const divBlock = document.createElement('div')
divBlock.classList.add('div-block');
divContainer.appendChild(divBlock);
}
}
// getting the div-block element
const divBlocks = document.querySelectorAll('.div-block');
divBlocks.forEach(block => block.addEventListener('mouseover', hoverMouse));
}
}
// function for mouse hover
function hoverMouse(e) {
if(customColor == "default") {
// default is grey background
e.target.classList.add('grey-bg');
} else if(customColor == "custom") {
// dynamically create background style
e.target.setAttribute("style", `background-color: ${customBackground.slice(customBackground.length - 1)}`)
} else if(customColor == "rainbow"){
e.target.setAttribute("style", "background-color: #" + (Math.random()*0xFFFFFF<<0).toString(16));
// '#'+(Math.random()*0xFFFFFF<<0).toString(16);
}
}
// color change function
function colorChanger() {
customBackground.push(this.value);
customColor = "custom";
}
// random color function
function randomColor(){
customColor = "rainbow";
}
// ask user for grid size
gridButton.addEventListener('click', changeGrid);
// color button event
getColorButton.addEventListener('change', colorChanger);
// event listener for random color button
randomColorButton.addEventListener('click', randomColor);