-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (68 loc) · 1.86 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
let squareViewWidth = 5;
let squareWidth = $(window).width() / (100/ squareViewWidth);
let colors = ['yellow', '#000', '#000','#000','#000','#000','#000','#ff8686', '#57baff']
$( document ).ready(fillScreen);
let scrolling = false;
$( document).scroll(function(){
if (!scrolling){
pickColor();
scrolling = true;
setTimeout(function() {
scrolling = false;
}, 500);
}
});
function fillScreen() {
// if (window.screen.width < 551) {
// squareViewWidth = 20;
// }
pickColor();
let amount = $(window).height() / squareWidth;
for (var i=0; i < amount; i++) {
addRow(i);
}
flipRandomSquares(amount * 12);
}
function pickColor(){
let colorIndex = Math.floor(Math.random() * colors.length)
var r = document.querySelector(':root');
r.style.setProperty('--state-2-color', colors[colorIndex]);
}
function flipRandomSquares(amount) {
let squares = $('.square');
let index;
for (let i = 0; i <amount; i++){
index = Math.floor(Math.random() * squares.length)
flip($(squares[index]));
}
}
function addRow(i) {
let amount = 100 / squareViewWidth;
let row = $('<div></div>');
row.addClass('row');
row.data('number', i);
for (var i=0; i < amount; i++) {
createSquare(row, i);
}
$('.container').append(row);
}
function createSquare(row, i){
let sq = $('<div></div>');
sq.addClass('square');
sq.addClass('state-1');
sq.data('number', i);
sq.mouseenter(function() {flip(sq)});
let innerCircle = $('<div></div>');
innerCircle.addClass('full');
sq.append(innerCircle);
row.append(sq);
}
function flip(sq) {
if (sq.hasClass('state-1')) {
sq.removeClass('state-1');
sq.addClass('state-2');
} else {
sq.removeClass('state-2');
sq.addClass('state-1');
}
}