-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-stars.js
45 lines (36 loc) · 1002 Bytes
/
random-stars.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
const spaceElement = document.querySelector('.space')
function randInt(min, max) {
return Math.floor(Math.random() * (max-min)) + min
}
function createStars(countStars) {
const count = countStars
let i = 0
let star
while (i <= count) {
star = document.createElement('span')
star.setAttribute('class', 'star')
spaceElement.appendChild(star)
i++
}
}
function addValues(animationTimeMS) {
const elements = document.querySelectorAll('.star')
const animationTime = animationTimeMS
elements.forEach(star => {
let x = randInt(0, window.innerWidth)
let y = 20
let duration = randInt(100, animationTime)
let height = randInt(0, 20)
star.style.left = x + 'px'
star.style.height = height + 'px'
star.style.width = 1 + 'px'
star.style.animationDuration = duration + 'ms'
})
}
const count = 40
const animationTimeMS = 300
function init() {
createStars(count)
addValues(animationTimeMS)
}
window.addEventListener('load', init)