Skip to content

Instantly share code, notes, and snippets.

@green6erry
Created February 11, 2020 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save green6erry/f4f9a6cc93138fcdb2fbb280b91cd769 to your computer and use it in GitHub Desktop.
Save green6erry/f4f9a6cc93138fcdb2fbb280b91cd769 to your computer and use it in GitHub Desktop.
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