init snowflakes via js

This commit is contained in:
MLH
2024-12-07 18:47:08 +01:00
parent bed9c109dc
commit 2f8f956987
2 changed files with 23 additions and 17 deletions

View File

@ -1,5 +1,6 @@
const snowflakes = true; // enable/disable snowflakes
const randomSnowflakes = true; // enable random Snowflakes
const randomSnowflakesMobile = false; // enable random Snowflakes on mobile devices
const enableColoredSnowflakes = true; // enable colored snowflakes on mobile devices
const snowflakeCount = 25; // count of random extra snowflakes
@ -45,7 +46,7 @@ observer.observe(document.body, {
function addRandomSnowflakes(count) {
const snowflakeContainer = document.querySelector('.snowflakes'); // get the snowflake container
if (!snowflakeContainer) return; // exit if snowflake container is not found
console.log('Adding random snowflakes');
const snowflakeSymbols = ['❅', '❆']; // some snowflake symbols
@ -80,13 +81,31 @@ function addRandomSnowflakes(count) {
console.log('Random snowflakes added');
}
// initialize standard snowflakes
function initSnowflakes() {
const snowflakesContainer = document.querySelector('.snowflakes');
// Array of snowflake characters
const snowflakeSymbols = ['❅', '❆'];
// create the 12 standard snowflakes
for (let i = 0; i < 12; i++) {
const snowflake = document.createElement('div');
snowflake.className = 'snowflake';
snowflake.textContent = snowflakeSymbols[i % 2]; // change between ❅ and ❆
snowflakesContainer.appendChild(snowflake);
}
}
// initialize snowflakes and add random snowflakes after the DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
if (!snowflakes) return; // exit if snowflakes are disabled
initSnowflakes();
toggleSnowflakes();
const screenWidth = window.innerWidth; // get the screen width to detect mobile devices
if (randomSnowflakes && screenWidth > 768) { // add random snowflakes only on larger screens
addRandomSnowflakes(snowflakeCount);
if (randomSnowflakes && screenWidth > 768 && randomSnowflakesMobile) { // add random snowflakes only on larger screens, unless enabled for mobile devices
addRandomSnowflakes(snowflakeCount);
}
});