Refactor snowflakes.js and snowflakes.css to streamline snowflake initialization and enhance mobile responsiveness
This commit is contained in:
@@ -2,8 +2,7 @@ const config = window.SeasonalsPluginConfig?.Snowflakes || {};
|
||||
|
||||
const snowflakes = config.EnableSnowflakes !== undefined ? config.EnableSnowflakes : true; // enable/disable snowflakes
|
||||
const snowflakeCount = config.SnowflakeCount !== undefined ? config.SnowflakeCount : 25; // count of snowflakes
|
||||
const randomSnowflakes = config.EnableRandomSnowflakes !== undefined ? config.EnableRandomSnowflakes : true; // enable random snowflakes
|
||||
const randomSnowflakesMobile = config.EnableRandomSnowflakesMobile !== undefined ? config.EnableRandomSnowflakesMobile : false; // enable random snowflakes on mobile
|
||||
const snowflakeCountMobile = config.SnowflakeCountMobile !== undefined ? config.SnowflakeCountMobile : 10; // count of snowflakes on mobile
|
||||
const enableColoredSnowflakes = config.EnableColoredSnowflakes !== undefined ? config.EnableColoredSnowflakes : true; // enable/disable colored snowflakes
|
||||
const enableDiffrentDuration = config.EnableDifferentDuration !== undefined ? config.EnableDifferentDuration : true; // enable different durations
|
||||
|
||||
@@ -46,11 +45,16 @@ observer.observe(document.body, {
|
||||
attributes: true
|
||||
});
|
||||
|
||||
function addRandomSnowflakes(count) {
|
||||
const snowflakeContainer = document.querySelector('.snowflakes'); // get the snowflake container
|
||||
if (!snowflakeContainer) return; // exit if snowflake container is not found
|
||||
function initSnowflakes(count) {
|
||||
let snowflakeContainer = document.querySelector('.snowflakes'); // get the snowflake container
|
||||
if (!snowflakeContainer) {
|
||||
snowflakeContainer = document.createElement("div");
|
||||
snowflakeContainer.className = "snowflakes";
|
||||
snowflakeContainer.setAttribute("aria-hidden", "true");
|
||||
document.body.appendChild(snowflakeContainer);
|
||||
}
|
||||
|
||||
console.log('Adding random snowflakes');
|
||||
console.log('Adding snowflakes');
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
// create a new snowflake element
|
||||
@@ -83,49 +87,18 @@ function addRandomSnowflakes(count) {
|
||||
// add the snowflake to the container
|
||||
snowflakeContainer.appendChild(snowflake);
|
||||
}
|
||||
console.log('Random snowflakes added');
|
||||
console.log('Snowflakes added');
|
||||
}
|
||||
|
||||
// initialize standard snowflakes
|
||||
function initSnowflakes() {
|
||||
const snowflakesContainer = document.querySelector('.snowflakes') || document.createElement("div");
|
||||
|
||||
if (!document.querySelector('.snowflakes')) {
|
||||
snowflakesContainer.className = "snowflakes";
|
||||
snowflakesContainer.setAttribute("aria-hidden", "true");
|
||||
document.body.appendChild(snowflakesContainer);
|
||||
}
|
||||
|
||||
// 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 ❆
|
||||
|
||||
// set random animation duration
|
||||
if (enableDiffrentDuration) {
|
||||
const randomAnimationDuration = Math.random() * 14 + 10; // delay (10s to 14s)
|
||||
const randomAnimationDuration2 = Math.random() * 5 + 3; // delay (3s to 5s)
|
||||
snowflake.style.animationDuration = `${randomAnimationDuration}s, ${randomAnimationDuration2}s`;
|
||||
}
|
||||
|
||||
snowflakesContainer.appendChild(snowflake);
|
||||
}
|
||||
}
|
||||
|
||||
// initialize snowflakes and add random snowflakes
|
||||
// initialize snowflakes
|
||||
function initializeSnowflakes() {
|
||||
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 || randomSnowflakesMobile)) { // add random snowflakes only on larger screens, unless enabled for mobile devices
|
||||
addRandomSnowflakes(snowflakeCount);
|
||||
}
|
||||
const isMobile = window.matchMedia("only screen and (max-width: 768px)").matches;
|
||||
const count = !isMobile ? snowflakeCount : snowflakeCountMobile;
|
||||
|
||||
initSnowflakes(count);
|
||||
toggleSnowflakes();
|
||||
}
|
||||
|
||||
initializeSnowflakes();
|
||||
Reference in New Issue
Block a user