Created
December 8, 2019 03:47
-
-
Save thehack/fee9afe7a8b5ded10d0780c7c25663a1 to your computer and use it in GitHub Desktop.
Javscript for snow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>JavaScript Snow</h1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var quantity = 100; | |
var snowflakes = []; | |
function getRandomNumber(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function createSnowflake() { | |
var el = document.createElement("div"); | |
el.style.position = "fixed"; | |
el.style.zIndex = "999999"; | |
el.style.color = "white"; | |
el.style.fontSize = getRandomNumber(10, 20) + "px"; | |
el.style.top = getRandomNumber(0, window.innerHeight) + "px"; | |
el.style.left = getRandomNumber(0, window.innerWidth) + "px"; | |
el.innerHTML = "❄"; | |
el.speed = 33 / parseInt(el.style.fontSize); | |
return el; | |
} | |
function moveSnowflakes() { | |
for (var i = 0; i < quantity; i++) { | |
moveSnowflake(snowflakes[i]); | |
} | |
} | |
function moveSnowflake(el) { | |
var height = window.innerHeight; | |
var top = parseInt(el.style.top); | |
//top += speed; | |
if (top > height) { | |
resetSnowflake(el); | |
} else { | |
el.style.top = top + el.speed + "px"; | |
} | |
} | |
function resetSnowflake(el) { | |
el.style.top = "0px"; | |
el.style.left = getRandomNumber(0, window.innerWidth) + "px"; | |
} | |
for (var i = 0; i < quantity; i++) { | |
particle = snowflakes[i] = createSnowflake(); | |
document.body.appendChild(particle); | |
} | |
setInterval(moveSnowflakes, 33); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body{background-color:#37474F} | |
h1 { | |
margin-top:42vh; | |
color:#E1F5FE; | |
text-align:center; | |
font-size:500%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment