-
Notifications
You must be signed in to change notification settings - Fork 50
/
bookshelf.js
45 lines (35 loc) · 1.22 KB
/
bookshelf.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
import { getRootCssStyles} from './cssUtils.js';
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomChoice(array) {
return array[Math.floor(Math.random() * array.length)];
}
let spines = Object.values(document.getElementsByClassName("spine"));
let covers = Object.values(document.getElementsByClassName("cover"));
let tops = Object.values(document.getElementsByClassName("top"));
let availablePatterns = getRootCssStyles();
let availableColors = [
"maroon",
"darkgreen",
"darkolivegreen",
"brown",
"saddlebrown",
"sienna",
"midnightblue",
];
// assign a random height, pattern and colour to each book
spines.map(function (s, i) {
let randomHeight = getRandomInt(220, 290);
s.style.height = `${randomHeight}px`;
s.style.top = `${280 - randomHeight}px`;
let randomPattern = randomChoice(availablePatterns);
s.style.backgroundImage = `var(${randomPattern})`;
let randomColor = randomChoice(availableColors);
s.style.backgroundColor = randomColor;
covers[i].style.height = `${randomHeight}px`;
covers[i].style.top = `${280 - randomHeight}px`;
tops[i].style.top = `${280 - randomHeight}px`;
});