Created
February 11, 2020 20:40
-
-
Save green6erry/f4f9a6cc93138fcdb2fbb280b91cd769 to your computer and use it in GitHub Desktop.
This file contains 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
let makeConfetti = () => { | |
let { height } = document.body.getBoundingClientRect(); | |
const particleQty = 120; | |
let random = (max) => { | |
return Math.floor(Math.random() * max); | |
} | |
for (let i = 0; i < particleQty; i++) { | |
let particle = document.createElement('div'); | |
let maxWidth = window.innerWidth; | |
particle.classList.add('confetti'); | |
particle.style.marginLeft = `${random(maxWidth)}px`; | |
particle.style.animationDelay = `${random(60) / 10}s`; | |
particle.style.animationDuration = `${(random(60) / 10)+8}s`; | |
particle.style.backgroundColor = `rgb(${random(255)}, ${random(255)}, ${random(255)})`; | |
document.body.insertBefore(particle, document.body.childNodes[0]); | |
} | |
var cssAnimation = document.createElement('style'); | |
cssAnimation.type = 'text/css'; | |
var rules = document.createTextNode('@-webkit-keyframes confetti {' + | |
'from { top:0; }' + | |
'to { top:' + height + 'px; }' + | |
'}'); | |
cssAnimation.appendChild(rules); | |
document.head.appendChild(cssAnimation); | |
} | |
console.log('started outside any fx'); | |
function ready(fn) { | |
if (document.readyState != 'loading') { | |
console.log('started inside ready'); | |
fn(); | |
} else { | |
document.addEventListener('DOMContentLoaded', fn); | |
} | |
} | |
ready(makeConfetti); | |
makeConfetti(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment