MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
No edit summary Tag: Manual revert |
||
Line 5: | Line 5: | ||
var container = document.body; | var container = document.body; | ||
var numberOfSnowflakes = 50; | var numberOfSnowflakes = 50; | ||
var maxAccumulatedHeight = 200; | |||
var accumulatedHeight = 0; | |||
for (var i = 0; i < numberOfSnowflakes; i++) { | for (var i = 0; i < numberOfSnowflakes; i++) { | ||
Line 24: | Line 26: | ||
function animateSnowflake(snowflake) { | function animateSnowflake(snowflake) { | ||
var speed = 1 + Math.random() * 2; | var speed = 1 + Math.random() * 2; | ||
var startX = Math.random() * window.innerWidth; | |||
var startY = Math.random() * window.innerHeight; | |||
snowflake.style.left = startX + "px"; | |||
snowflake.style.top = startY + "px"; | |||
function moveSnowflake() { | function moveSnowflake() { | ||
var currentTop = parseFloat(snowflake.style.top) | var currentTop = parseFloat(snowflake.style.top); | ||
var newTop = currentTop + speed; | var newTop = currentTop + speed; | ||
Line 32: | Line 39: | ||
if (newTop > window.innerHeight) { | if (newTop > window.innerHeight) { | ||
snowflake.style.top = " | accumulatedHeight += window.innerHeight; | ||
snowflake.style.top = -accumulatedHeight + "px"; | |||
snowflake.style.left = Math.random() * window.innerWidth + "px"; | snowflake.style.left = Math.random() * window.innerWidth + "px"; | ||
if (accumulatedHeight >= maxAccumulatedHeight) { | |||
accumulatedHeight = 0; | |||
} | |||
} | } | ||
Revision as of 22:59, 4 December 2023
/* Any JavaScript here will be loaded for all users on every page load. */ // Wrap the code in a function to avoid polluting the global namespace function initSnowfall() { var container = document.body; var numberOfSnowflakes = 50; var maxAccumulatedHeight = 200; var accumulatedHeight = 0; for (var i = 0; i < numberOfSnowflakes; i++) { createSnowflake(); } function createSnowflake() { var snowflake = document.createElement("div"); snowflake.className = "snowflake"; container.appendChild(snowflake); var size = Math.random() * 5 + 3; snowflake.style.width = size + "px"; snowflake.style.height = size + "px"; animateSnowflake(snowflake); } function animateSnowflake(snowflake) { var speed = 1 + Math.random() * 2; var startX = Math.random() * window.innerWidth; var startY = Math.random() * window.innerHeight; snowflake.style.left = startX + "px"; snowflake.style.top = startY + "px"; function moveSnowflake() { var currentTop = parseFloat(snowflake.style.top); var newTop = currentTop + speed; snowflake.style.top = newTop + "px"; if (newTop > window.innerHeight) { accumulatedHeight += window.innerHeight; snowflake.style.top = -accumulatedHeight + "px"; snowflake.style.left = Math.random() * window.innerWidth + "px"; if (accumulatedHeight >= maxAccumulatedHeight) { accumulatedHeight = 0; } } requestAnimationFrame(moveSnowflake); } moveSnowflake(); } } // Call the initialization function when the DOM is ready $(document).ready(initSnowfall);