You can copy and run this file directly in your browser.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Animated Weather Cards</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: #181c24;
min-height: 100vh;
margin: 0;
font-family: 'SF Pro Display', 'Segoe UI', Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
color: #fff;
}
h1 {
margin-top: 32px;
margin-bottom: 16px;
font-weight: 700;
letter-spacing: 1px;
}
.weather-controls {
margin-bottom: 32px;
display: flex;
gap: 12px;
}
.weather-controls button {
background: #232a36;
color: #fff;
border: none;
border-radius: 6px;
padding: 10px 22px;
font-size: 1rem;
cursor: pointer;
transition: background 0.2s;
font-weight: 500;
letter-spacing: 0.5px;
}
.weather-controls button.active,
.weather-controls button:hover {
background: #3a4457;
}
.weather-cards {
display: flex;
gap: 32px;
flex-wrap: wrap;
justify-content: center;
margin-bottom: 40px;
}
.weather-card {
background: #232a36;
border-radius: 20px;
box-shadow: 0 6px 32px rgba(0,0,0,0.25);
width: 260px;
height: 340px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-end;
position: relative;
overflow: hidden;
transition: background 0.5s;
}
.weather-title {
font-size: 1.2rem;
font-weight: 600;
margin-bottom: 18px;
letter-spacing: 1px;
z-index: 2;
text-shadow: 0 2px 8px #0008;
}
.weather-anim {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
width: 100%; height: 100%;
pointer-events: none;
z-index: 1;
}
/* Sun background */
.weather-card.sun {
background: linear-gradient(180deg, #232a36 60%, #f7d774 100%);
}
/* Rain background */
.weather-card.rain {
background: linear-gradient(180deg, #232a36 60%, #3a4457 100%);
}
/* Wind background */
.weather-card.wind {
background: linear-gradient(180deg, #232a36 60%, #2e3a4d 100%);
}
/* Snow background */
.weather-card.snow {
background: linear-gradient(180deg, #232a36 60%, #b3c6e0 100%);
}
</style>
</head>
<body>
<h1>Animated Weather Cards</h1>
<div class="weather-controls">
<button data-weather="wind" class="active">Wind</button>
<button data-weather="rain">Rain</button>
<button data-weather="sun">Sun</button>
<button data-weather="snow">Snow</button>
</div>
<div class="weather-cards">
<div class="weather-card wind" data-card>
<div class="weather-title">Wind</div>
<div class="weather-anim" data-anim></div>
</div>
<div class="weather-card rain" data-card>
<div class="weather-title">Rain</div>
<div class="weather-anim" data-anim></div>
</div>
<div class="weather-card sun" data-card>
<div class="weather-title">Sun</div>
<div class="weather-anim" data-anim></div>
</div>
<div class="weather-card snow" data-card>
<div class="weather-title">Snow</div>
<div class="weather-anim" data-anim></div>
</div>
</div>
<script>
// --- Weather Animation Renderers ---
// Each function receives a container and renders the animation inside
// Utility: Clear container
function clearAnim(container) {
container.innerHTML = '';
}
// --- WIND ---
function renderWind(container) {
clearAnim(container);
// SVG for clouds and tree
container.innerHTML = `
<svg width="100%" height="100%" viewBox="0 0 260 340">
<!-- Wind lines -->
<g>
<path id="wind1" d="M30 80 Q80 90 130 80 Q180 70 230 90" stroke="#b3e0ff" stroke-width="4" fill="none" opacity="0.7"/>
<path id="wind2" d="M50 120 Q110 130 170 120 Q210 110 240 130" stroke="#b3e0ff" stroke-width="3" fill="none" opacity="0.5"/>
</g>
<!-- Clouds -->
<g id="clouds">
<ellipse id="cloud1" cx="60" cy="60" rx="32" ry="18" fill="#e0f7fa" opacity="0.85"/>
<ellipse id="cloud2" cx="180" cy="50" rx="28" ry="15" fill="#b3e0ff" opacity="0.7"/>
</g>
<!-- Tree trunk -->
<rect x="200" y="210" width="10" height="60" rx="4" fill="#6d4c41"/>
<!-- Tree leaves -->
<g id="tree-leaves">
<ellipse cx="205" cy="210" rx="22" ry="18" fill="#7ed957"/>
<ellipse cx="215" cy="220" rx="14" ry="10" fill="#5cb85c"/>
</g>
</svg>
`;
// Animate clouds and tree
const cloud1 = container.querySelector('#cloud1');
const cloud2 = container.querySelector('#cloud2');
const treeLeaves = container.querySelector('#tree-leaves');
let t = 0;
function animate() {
t += 0.016;
// Move clouds horizontally
cloud1.setAttribute('cx', 60 + 20 * Math.sin(t * 0.7));
cloud2.setAttribute('cx', 180 + 25 * Math.sin(t * 0.5 + 1));
// Sway tree leaves
treeLeaves.setAttribute('transform', `rotate(${Math.sin(t*2)*8},205,220)`);
requestAnimationFrame(animate);
}
animate();
}
// --- RAIN ---
function renderRain(container) {
clearAnim(container);
// SVG for clouds, raindrops, puddle
container.innerHTML = `
<svg width="100%" height="100%" viewBox="0 0 260 340">
<!-- Cloud -->
<ellipse cx="130" cy="70" rx="60" ry="28" fill="#b3c6e0" opacity="0.85"/>
<ellipse cx="170" cy="80" rx="32" ry="16" fill="#e0e7ef" opacity="0.7"/>
<ellipse cx="90" cy="80" rx="28" ry="14" fill="#e0e7ef" opacity="0.7"/>
<!-- Puddle -->
<ellipse id="puddle" cx="130" cy="300" rx="50" ry="12" fill="#7ecfff" opacity="0.4"/>
<!-- Raindrops group -->
<g id="raindrops"></g>
</svg>
`;
// Animate raindrops
const raindrops = container.querySelector('#raindrops');
const puddle = container.querySelector('#puddle');
const dropCount = 18;
let drops = [];
for (let i = 0; i < dropCount; i++) {
let x = 60 + Math.random() * 140;
let y = 90 + Math.random() * 120;
let speed = 2 + Math.random() * 1.5;
let len = 16 + Math.random() * 8;
let drop = document.createElementNS("http://www.w3.org/2000/svg", "line");
drop.setAttribute('x1', x);
drop.setAttribute('y1', y);
drop.setAttribute('x2', x);
drop.setAttribute('y2', y + len);
drop.setAttribute('stroke', '#7ecfff');
drop.setAttribute('stroke-width', 2.2);
drop.setAttribute('stroke-linecap', 'round');
drop.setAttribute('opacity', 0.7 + Math.random()*0.3);
raindrops.appendChild(drop);
drops.push({el: drop, x, y, len, speed});
}
let puddleGrow = 0;
function animate() {
let anyHit = false;
for (let d of drops) {
d.y += d.speed;
if (d.y > 290) {
d.y = 90 + Math.random() * 40;
d.x = 60 + Math.random() * 140;
d.speed = 2 + Math.random() * 1.5;
d.len = 16 + Math.random() * 8;
d.el.setAttribute('opacity', 0.7 + Math.random()*0.3);
anyHit = true;
}
d.el.setAttribute('x1', d.x);
d.el.setAttribute('x2', d.x);
d.el.setAttribute('y1', d.y);
d.el.setAttribute('y2', d.y + d.len);
}
// Animate puddle growing slightly when raindrops hit
if (anyHit) {
puddleGrow = 1;
}
let rx = 50 + puddleGrow * 6;
let ry = 12 + puddleGrow * 2;
puddle.setAttribute('rx', rx);
puddle.setAttribute('ry', ry);
puddleGrow *= 0.92;
requestAnimationFrame(animate);
}
animate();
}
// --- SUN ---
function renderSun(container) {
clearAnim(container);
// SVG for sun and rays
container.innerHTML = `
<svg width="100%" height="100%" viewBox="0 0 260 340">
<!-- Sun rays group -->
<g id="rays"></g>
<!-- Sun core -->
<circle id="sun" cx="130" cy="110" r="38" fill="url(#sunGrad)" filter="url(#glow)"/>
<defs>
<radialGradient id="sunGrad" cx="50%" cy="50%" r="50%">
<stop offset="0%" stop-color="#fffde4"/>
<stop offset="60%" stop-color="#ffe066"/>
<stop offset="100%" stop-color="#f7d774"/>
</radialGradient>
<filter id="glow" x="-50%" y="-50%" width="200%" height="200%">
<feGaussianBlur stdDeviation="8" result="coloredBlur"/>
<feMerge>
<feMergeNode in="coloredBlur"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
</defs>
</svg>
`;
// Animate rays
const rays = container.querySelector('#rays');
const rayCount = 12;
let raysArr = [];
for (let i = 0; i < rayCount; i++) {
let angle = (i / rayCount) * Math.PI * 2;
let x1 = 130 + Math.cos(angle) * 48;
let y1 = 110 + Math.sin(angle) * 48;
let x2 = 130 + Math.cos(angle) * 80;
let y2 = 110 + Math.sin(angle) * 80;
let ray = document.createElementNS("http://www.w3.org/2000/svg", "line");
ray.setAttribute('x1', x1);
ray.setAttribute('y1', y1);
ray.setAttribute('x2', x2);
ray.setAttribute('y2', y2);
ray.setAttribute('stroke', '#ffe066');
ray.setAttribute('stroke-width', 7);
ray.setAttribute('stroke-linecap', 'round');
ray.setAttribute('opacity', 0.7);
rays.appendChild(ray);
raysArr.push(ray);
}
let t = 0;
function animate() {
t += 0.016;
for (let i = 0; i < rayCount; i++) {
let angle = (i / rayCount) * Math.PI * 2 + t * 0.5;
let len = 32 + 10 * Math.sin(t*1.2 + i);
let x1 = 130 + Math.cos(angle) * 48;
let y1 = 110 + Math.sin(angle) * 48;
let x2 = 130 + Math.cos(angle) * (48 + len);
let y2 = 110 + Math.sin(angle) * (48 + len);
raysArr[i].setAttribute('x1', x1);
raysArr[i].setAttribute('y1', y1);
raysArr[i].setAttribute('x2', x2);
raysArr[i].setAttribute('y2', y2);
raysArr[i].setAttribute('opacity', 0.6 + 0.3 * Math.sin(t*1.2 + i));
}
requestAnimationFrame(animate);
}
animate();
}
// --- SNOW ---
function renderSnow(container) {
clearAnim(container);
// SVG for snowflakes and snow ground
container.innerHTML = `
<svg width="100%" height="100%" viewBox="0 0 260 340">
<!-- Snow ground -->
<ellipse id="snowground" cx="130" cy="310" rx="60" ry="18" fill="#fff" opacity="0.85"/>
<!-- Snowflakes group -->
<g id="flakes"></g>
</svg>
`;
const flakes = container.querySelector('#flakes');
const snowground = container.querySelector('#snowground');
const flakeCount = 22;
let snowArr = [];
for (let i = 0; i < flakeCount; i++) {
let x = 40 + Math.random() * 180;
let y = 60 + Math.random() * 180;
let r = 2.5 + Math.random() * 2.5;
let speed = 0.7 + Math.random() * 0.7;
let drift = 0.5 + Math.random() * 0.7;
let flake = document.createElementNS("http://www.w3.org/2000/svg", "circle");
flake.setAttribute('cx', x);
flake.setAttribute('cy', y);
flake.setAttribute('r', r);
flake.setAttribute('fill', '#fff');
flake.setAttribute('opacity', 0.8 + Math.random()*0.2);
flakes.appendChild(flake);
snowArr.push({el: flake, x, y, r, speed, drift, phase: Math.random()*Math.PI*2});
}
let snowAccum = 0;
function animate() {
let anyHit = false;
for (let s of snowArr) {
s.y += s.speed;
s.x += Math.sin(Date.now()/700 + s.phase) * s.drift;
if (s.y > 300) {
s.y = 60 + Math.random() * 40;
s.x = 40 + Math.random() * 180;
s.speed = 0.7 + Math.random() * 0.7;
s.drift = 0.5 + Math.random() * 0.7;
s.phase = Math.random()*Math.PI*2;
anyHit = true;
}
s.el.setAttribute('cx', s.x);
s.el.setAttribute('cy', s.y);
}
// Animate snow ground accumulating
if (anyHit) {
snowAccum = Math.min(1, snowAccum + 0.08);
}
let rx = 60 + snowAccum * 10;
let ry = 18 + snowAccum * 4;
snowground.setAttribute('rx', rx);
snowground.setAttribute('ry', ry);
snowAccum *= 0.995;
requestAnimationFrame(animate);
}
animate();
}
// --- Card Weather Switcher ---
const weatherTypes = ['wind', 'rain', 'sun', 'snow'];
const renderers = {
wind: renderWind,
rain: renderRain,
sun: renderSun,
snow: renderSnow
};
// Initial render: each card shows its own weather
document.querySelectorAll('.weather-card').forEach((card, i) => {
const anim = card.querySelector('[data-anim]');
const type = weatherTypes[i];
renderers[type](anim);
});
// Controls: Switch all cards to the same weather
document.querySelectorAll('.weather-controls button').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.weather-controls button').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
const type = btn.getAttribute('data-weather');
document.querySelectorAll('.weather-card').forEach(card => {
card.className = 'weather-card ' + type;
card.querySelector('.weather-title').textContent = type.charAt(0).toUpperCase() + type.slice(1);
renderers[type](card.querySelector('[data-anim]'));
});
});
});
</script>
</body>
</html>