-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (69 loc) · 2.33 KB
/
script.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
68
69
70
71
72
73
74
75
const grid=document.querySelector('.grid')
createGrid=()=>{
for(let i=0;i<256;i++){
const div=document.createElement('div');
div.classList.add('cell');
div.addEventListener('mouseover',function(event){
div.style.backgroundColor='black';
})
grid.appendChild(div);
}
}
function removeAllCell(parent){
while(parent.firstChild){
parent.removeChild(parent.firstChild);
}
}
function getRandomColor(){
let temp=Math.floor(Math.random()*16777215).toString(16);
temp='#'+temp;
return temp;
}
const colorSpecific=document.querySelector('#color')
colorSpecific.addEventListener('input',function(event){
const colorVal=document.getElementById('color').value;
let val=document.getElementById('slider').value;
let cell=Array.from(grid.children);
for(let i=0;i<val*val;i++){
cell[i].addEventListener('mouseover',function(event){
event.target.style.backgroundColor=colorVal;
})
}
})
const black=document.querySelector('#black');
black.addEventListener('click',function(){
let val=document.getElementById('slider').value;
let cell=grid.children;
for(let i=0;i<val*val;i++){
cell[i].addEventListener('mouseover',function(event){
event.target.style.backgroundColor='black';
})
}
})
const randomColor=document.querySelector('#rgb');
randomColor.addEventListener('click',function(){
let val = document.getElementById('slider').value;
let cell = grid.children;
for (let i = 0; i < val*val; i++) {
cell[i].addEventListener('mouseover', function(event){
event.target.style.backgroundColor = getRandomColor();
})
}
});
const slider=document.querySelector('#slider');
const screenVal=document.querySelector('#value');
slider.addEventListener('input',function(){
let x=document.getElementById('slider').value;
screenVal.textContent=x;
removeAllCell(grid);
grid.setAttribute('style', `grid-template-columns: repeat(${x}, 2fr); grid-template-rows: repeat(${x}, 2fr);`);
for(let i=0;i<x*x;i++){
const div=document.createElement('div');
div.classList.add('cell');
div.addEventListener('mouseover',function(event){
event.target.style.backgroundColor='black';
})
grid.appendChild(div);
}
});
createGrid();