你是顶级前端工程师,现就职于apple. Create a single HTML file containing CSS and JavaScript to generate…
- User
你是顶级前端工程师,现就职于apple.
Create a single HTML file containing CSS and JavaScript to generate an animated weather card. The card should visually represent the following weather conditions with distinct animations: Wind: (e.g., moving clouds, swaying trees, or wind lines) Rain: (e.g., falling raindrops, puddles forming) Sun: (e.g., shining rays, bright background) Snow: (e.g., falling snowflakes, snow accumulating) Show all the weather card side by side The card should have a dark background. Provide all the HTML, CSS, and JavaScript code within this single file. The JavaScript should include a way to switch between the different weather conditions (e.g., a function or a set of buttons) to demonstrate the animations for each.
Assistant
Okay, here's a solution crafted with the precision and attention to detail you'd expect from Apple. We'll create a single HTML file incorporating HTML structure, CSS for styling and animation, and a touch of JavaScript to enhance the dynamic elements like rain and snow.
This file will display four weather cards side-by-side, each representing a different condition (Windy, Rainy, Sunny, Snowy) with continuous, distinct animations against a dark background.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Animated Weather Cards</title> <style> /* --- Global Styles & Setup --- */ :root { --card-width: 220px; --card-height: 350px; --dark-bg: #1a1a1d; /* Slightly richer dark */ --card-bg: #2c2c31; /* Dark grey card */ --text-color: #f0f0f0; --shadow-color: rgba(0, 0, 0, 0.4); --cloud-color: #f1f1f1; --rain-color: #87ceeb; /* Sky blue */ --sun-color: #ffeb3b; /* Bright yellow */ --sun-glow: #ffc107; /* Amber */ --snow-color: #ffffff; } *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; background-color: var(--dark-bg); color: var(--text-color); display: flex; justify-content: center; align-items: center; min-height: 100vh; padding: 30px; flex-wrap: wrap; /* Allow cards to wrap on smaller screens */ gap: 30px; /* Spacing between cards */ } /* --- Base Card Style --- */ .weather-card { width: var(--card-width); height: var(--card-height); background: var(--card-bg); border-radius: 20px; /* Smooth Apple-like curves */ box-shadow: 0 8px 25px var(--shadow-color); padding: 25px; position: relative; /* Crucial for positioning animated elements */ overflow: hidden; /* Keep animations contained */ display: flex; flex-direction: column; align-items: center; text-align: center; transition: transform 0.3s ease, box-shadow 0.3s ease; } .weather-card:hover { transform: translateY(-5px); box-shadow: 0 12px 35px var(--shadow-color); } .weather-card h2 { margin-bottom: 15px; font-size: 1.4em; font-weight: 500; z-index: 10; /* Keep title above some elements */ } .animation-container { flex-grow: 1; /* Take remaining space */ width: 100%; position: relative; } /* --- Base Animated Element Styles --- */ .animated-element { position: absolute; pointer-events: none; /* Prevent interference */ } /* --- Windy Condition --- */ .weather-card.windy .cloud { width: 80px; height: 30px; background: var(--cloud-color); border-radius: 20px; position: absolute; opacity: 0.8; animation: moveCloud 15s linear infinite; } .weather-card.windy .cloud::before, .weather-card.windy .cloud::after { content: ''; position: absolute; background: var(--cloud-color); border-radius: 50%; } .weather-card.windy .cloud::before { width: 40px; height: 40px; top: -20px; left: 15px; } .weather-card.windy .cloud::after { width: 50px; height: 50px; top: -25px; right: 10px; } .weather-card.windy .cloud.c1 { top: 40%; left: -100px; animation-duration: 12s; transform: scale(0.8); animation-delay: -3s;} .weather-card.windy .cloud.c2 { top: 55%; left: -120px; animation-duration: 15s; transform: scale(1); animation-delay: 0s;} .weather-card.windy .cloud.c3 { top: 70%; left: -80px; animation-duration: 10s; transform: scale(0.6); animation-delay: -6s;} @keyframes moveCloud { 0% { transform: translateX(0); } 100% { transform: translateX(calc(var(--card-width) + 150px)); } /* Move across and off card */ } /* --- Rainy Condition --- */ .weather-card.rainy .raindrop { width: 1px; /* Thinner raindrops */ height: 15px; /* Longer raindrops */ background: linear-gradient(to bottom, rgba(135, 206, 235, 0), var(--rain-color)); /* Faded top */ position: absolute; animation: fall linear infinite; border-bottom-left-radius: 1px; border-bottom-right-radius: 1px; } @keyframes fall { 0% { transform: translateY(-50px); opacity: 1; } 90% { opacity: 1;} 100% { transform: translateY(var(--card-height)); opacity: 0; } /* Fall below card */ } /* --- Sunny Condition --- */ .weather-card.sunny { background: linear-gradient(to bottom, #4a709b, #87ceeb); /* Blue sky gradient */ } .weather-card.sunny h2 { color: #fff; /* White text on blue sky */ text-shadow: 1px 1px 3px rgba(0,0,0,0.2); } .weather-card.sunny .sun { width: 80px; height: 80px; background-color: var(--sun-color); border-radius: 50%; position: absolute; top: 30%; left: 50%; transform: translateX(-50%); box-shadow: 0 0 30px 10px var(--sun-glow); animation: pulse 3s ease-in-out infinite; } .weather-card.sunny .sun::before { /* Subtle rays effect */ content: ''; position: absolute; top: -20px; left: -20px; right: -20px; bottom: -20px; border: 2px solid rgba(255, 235, 59, 0.3); border-radius: 50%; animation: radiate 3s ease-in-out infinite 0.5s; opacity: 0; } .weather-card.sunny .sun::after { /* Second layer */ content: ''; position: absolute; top: -30px; left: -30px; right: -30px; bottom: -30px; border: 1px solid rgba(255, 235, 59, 0.2); border-radius: 50%; animation: radiate 3s ease-in-out infinite 1s; opacity: 0; } @keyframes pulse { 0%, 100% { transform: translateX(-50%) scale(1); box-shadow: 0 0 30px 10px var(--sun-glow); } 50% { transform: translateX(-50%) scale(1.05); box-shadow: 0 0 40px 15px var(--sun-glow); } } @keyframes radiate { 0% { transform: scale(0.8); opacity: 0; } 50% { opacity: 1; } 100% { transform: scale(1.2); opacity: 0; } } /* --- Snowy Condition --- */ .weather-card.snowy .snowflake { width: 8px; height: 8px; background-color: var(--snow-color); border-radius: 50%; position: absolute; top: -10px; /* Start above card */ animation: fallSnow linear infinite; opacity: 0.9; box-shadow: 0 0 5px rgba(255, 255, 255, 0.5); } @keyframes fallSnow { 0% { transform: translate(0, 0) rotate(0deg); opacity: 1; } 100% { /* Adjust X for drift, Y for falling distance */ transform: translate(var(--drift, 0px), var(--card-height)) rotate(var(--spin, 0deg)); opacity: 0; } } </style> </head> <body> <!-- Windy Card --> <div class="weather-card windy"> <h2>Windy</h2> <div class="animation-container"> <div class="cloud c1"></div> <div class="cloud c2"></div> <div class="cloud c3"></div> </div> </div> <!-- Rainy Card --> <div class="weather-card rainy"> <h2>Rainy</h2> <div class="animation-container" id="rain-container"> <!-- Raindrops will be added by JavaScript --> </div> </div> <!-- Sunny Card --> <div class="weather-card sunny"> <h2>Sunny</h2> <div class="animation-container"> <div class="sun"></div> </div> </div> <!-- Snowy Card --> <div class="weather-card snowy"> <h2>Snowy</h2> <div class="animation-container" id="snow-container"> <!-- Snowflakes will be added by JavaScript --> </div> </div> <script> // --- JavaScript for Dynamic Elements --- // We use JS to create a more natural distribution of rain/snow function createDynamicElements(containerId, elementClass, count, cardWidth, cardHeight) { const container = document.getElementById(containerId); if (!container) return; for (let i = 0; i < count; i++) { const element = document.createElement('div'); element.classList.add('animated-element', elementClass); // Random horizontal position const randomLeft = Math.random() * cardWidth; element.style.left = `${randomLeft}px`; // Random animation duration and delay for variation const randomDuration = Math.random() * 3 + 2; // e.g., 2s to 5s const randomDelay = Math.random() * 5; // Delay up to 5s element.style.animationDuration = `${randomDuration}s`; element.style.animationDelay = `-${randomDelay}s`; // Negative delay starts animation partway through // Specific adjustments for snow if (elementClass === 'snowflake') { const randomSize = Math.random() * 4 + 4; // Size 4px to 8px element.style.width = `${randomSize}px`; element.style.height = `${randomSize}px`; // Add random horizontal drift and spin const drift = (Math.random() - 0.5) * 100; // Pixels left/right const spin = (Math.random() - 0.5) * 720; // Degrees rotation element.style.setProperty('--drift', `${drift}px`); element.style.setProperty('--spin', `${spin}deg`); } // Set card height as a CSS variable for animations if needed (e.g., fall distance) // The keyframe already uses var(--card-height), ensure it's defined in CSS or set here if needed globally // element.style.setProperty('--card-height', `${cardHeight}px`); // Example if needed per element container.appendChild(element); } } // --- Initialize Animations --- document.addEventListener('DOMContentLoaded', () => { // Get computed style to accurately determine card dimensions const cardStyle = getComputedStyle(document.querySelector('.weather-card')); // Extract numerical value from properties like '220px' const cardWidth = parseFloat(cardStyle.width); const cardHeight = parseFloat(cardStyle.height); // Adjust card height dynamically in CSS variable for animations // Setting it on the root makes it available globally document.documentElement.style.setProperty('--card-height', `${cardHeight}px`); // Create Raindrops createDynamicElements('rain-container', 'raindrop', 50, cardWidth, cardHeight); // 50 raindrops // Create Snowflakes createDynamicElements('snow-container', 'snowflake', 70, cardWidth, cardHeight); // 70 snowflakes }); </script> </body> </html>Explanation:
-
HTML Structure:
- Basic HTML5 boilerplate.
- A
<body>styled with Flexbox to center the cards and provide spacing (gap). - Four
divelements with the classweather-card. Each also has a unique class (windy,rainy,sunny,snowy) to target specific styles and animations. - Inside each card:
- An
<h2>for the title. - A
div.animation-containerto hold the animated elements. This helps separate the static title from the dynamic visuals. - Specific elements for animation are either predefined (like
.cloud,.sun) or generated by JavaScript (raindrops, snowflakes) and placed inside theanimation-container.
- An
-
CSS Styling:
:rootVariables: Define colors, dimensions, etc., for easy customization and consistency (a good practice).bodyStyling: Sets the dark background, font (using Apple's system font stack), and flexbox layout..weather-cardBase: Defines the common appearance (size, background, border-radius, shadow, padding,position: relative,overflow: hidden). Theposition: relativeandoverflow: hiddenare essential for containing the absolutely positioned animated elements. A subtle hover effect is added.h2Styling: Basic title styling..animation-container: Takes up the remaining vertical space in the card.- Condition-Specific Styles:
.windy: Styles the.cloudelements (shape, color, position) and applies themoveCloudanimation. Multiple clouds with varied delays/durations/sizes create a more dynamic scene..rainy: Styles the.raindropelements (thin line, gradient, color) and applies thefallanimation. The actual elements are added via JS..sunny: Sets a gradient background for the card itself. Styles the.sun(circle, color, shadow) and appliespulseandradiate(using pseudo-elements) animations for a gentle shining effect..snowy: Styles the.snowflakeelements (small white circle, shadow) and applies thefallSnowanimation. JS adds these elements. Custom properties (--drift,--spin) are used within the keyframes and set by JS for individual variation.
@keyframes: Define the animations (moveCloud,fall,pulse,radiate,fallSnow) that manipulate properties liketransform,opacity, andbox-shadow. Usingtransform(translateX,translateY,scale,rotate) is generally better for performance than animatingtop/left.
-
JavaScript:
createDynamicElementsFunction:- Takes the container ID, element class name, number of elements to create, and card dimensions as input.
- Loops
counttimes to createdivelements. - Assigns the base
animated-elementclass and the specific weather class (raindroporsnowflake). - Sets random
leftpositions for horizontal distribution. - Sets random
animation-durationandanimation-delay(using a negative delay to start animations immediately but offset in their cycle) for a less synchronized, more natural look. - For Snowflakes: Adds extra randomization for
width/height(size) and sets CSS custom properties (--drift,--spin) directly on the element. These variables are then used within thefallSnowkeyframes, allowing each snowflake to have unique drift and rotation. - Appends the created element to its designated container (
#rain-containeror#snow-container).
DOMContentLoadedEvent Listener:- Ensures the script runs only after the HTML is fully loaded.
- Gets the actual computed dimensions of the card using
getComputedStyle. This is more reliable than using the CSS variable directly, especially if browser defaults or other styles interfere. - Sets the
--card-heightCSS variable on the root element (documentElement) so thefallandfallSnowkeyframes know how far down the elements should travel before disappearing. - Calls
createDynamicElementsfor both rain and snow, specifying the number of elements desired.
This approach provides a clean, single-file solution with visually distinct and continuously running animations for each weather condition, displayed side-by-side as requested. The use of CSS variables and minimal, targeted JavaScript enhances the visual appeal and maintainability.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-3-5-haiku-20241022Compare side by side →
- gpt-4.1-2025-04-14Compare side by side →
llama-3.3-70b-instructCompare side by side →
- gpt-4o-mini-2024-07-18Compare side by side →
gemini-2.0-flash-001Compare side by side →
gemini-2.5-pro-preview-03-25Compare side by side →
claude-3-7-sonnet-20250219Compare side by side →
- o3-miniCompare side by side →
- gpt-4o-2024-11-20Compare side by side →
- chatgpt-4o-latest-20250326Compare side by side →
claude-3-5-sonnet-20241022Compare side by side →