From 16956dec4b260e81e1f2ebc8e9c46f0cfb0ce799 Mon Sep 17 00:00:00 2001 From: CodeDevMLH Date: Thu, 19 Dec 2024 00:36:23 +0100 Subject: [PATCH] add random --- halloween.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/halloween.js b/halloween.js index 08f7688..979c7dd 100644 --- a/halloween.js +++ b/halloween.js @@ -1,4 +1,7 @@ const halloween = true; // enable/disable halloween +const randomSymbols = true; // enable more random symbols +const randomSymbolsMobile = false; // enable random symbols on mobile devices +const halloweenCount = 25; // count of random extra symbols let msgPrinted = false; // flag to prevent multiple console messages @@ -75,9 +78,46 @@ function createHalloween() { } +function addRandomSymbols(count) { + const halloweenContainer = document.querySelector('.halloween-container'); // get the halloween container + if (!halloweenContainer) return; // exit if halloween container is not found + + console.log('Adding random halloween symbols'); + + + for (let i = 0; i < count; i++) { + // create a new halloween elements + const halloween = document.createElement('div'); + halloween.classList.add('halloween'); + + // pick a random halloween symbol + halloween.textContent = images[Math.floor(Math.random() * images.length)]; + + // set random horizontal position, animation delay and size(uncomment lines to enable) + const randomLeft = Math.random() * 100; // position (0% to 100%) + //const randomSize = Math.random() * 1.5 + 0.5; // size (0.5em to 2em) //uncomment to enable random size + const randomAnimationDelay = Math.random() * 8; // delay (0s to 8s) + const randomAnimationDelay2 = Math.random() * 5; // delay (0s to 5s) + + // apply styles + halloween.style.left = `${randomLeft}%`; + halloween.style.animationDelay = `${randomAnimationDelay}s, ${randomAnimationDelay2}s`; + + // add the halloween to the container + halloweenContainer.appendChild(halloween); + } + console.log('Random halloween symbols added'); +} + + // initialize halloween after the DOM is loaded document.addEventListener('DOMContentLoaded', () => { if (!halloween) return; // exit if halloween is disabled createHalloween(); - toogleHalloween(); + toggleHalloween(); + + const screenWidth = window.innerWidth; // get the screen width to detect mobile devices + if (randomSymbols && (screenWidth > 768 || randomSymbolsMobile)) { // add random halloweens only on larger screens, unless enabled for mobile devices + addRandomSymbols(halloweenCount); + } });