🧠 Description
A modern animated button built using HTML, CSS, and JavaScript that visually transitions through three states — idle, loading, and success. When clicked, the button replaces its text with a spinning loader animation, simulating a background process (like a form submission). After a short delay, the loader switches to a success checkmark before returning to its original state.
💻 HTML Code
HTML Structure Explanation
The HTML defines a single button element with the class animated-button. Inside it, there’s a wrapper "div" called button-content, which holds three main elements:
Loader SVG: A circular stroke animation that rotates to represent “loading”.
Checkmark SVG: A success indicator shown after loading completes.
Text span: Displays the default button label ("Click Me").
The SVG icons are scalable vector graphics — they allow for crisp animations without external image files.
The JavaScript manipulates these inner elements dynamically to switch between states.
CSS Code
Body Setup
The body uses Flexbox to center the button both vertically and horizontally.
The background is dark (#121212), giving the button contrast and focus.
Button Styling
The button has:
A green background (#22c55e — Tailwind’s “emerald” tone).
White text for contrast.
A pill shape achieved via a large border-radius (9999px).
Padding, bold font, and spacing between inner elements.
It uses inline-flex to align the icons and text horizontally.
Inner Elements
The .button-content is a flex container that holds the loader, checkmark, and text neatly aligned.
The .loader and .check are hidden by default (display: none and width: 0).
Both start at a scaled-down size (0.5) for a subtle effect.
Animations
The @keyframes rotate animation rotates the loader 360° infinitely while keeping it scaled down.
This rotation gives the visual cue that a process is happening.
Visual Flow
Initially: Only text ("Click Me") is visible.
During loading: The loader appears, spinning beside the faded text.
On success: The loader disappears, replaced by the checkmark icon.
After a short delay: The checkmark fades out, and the button resets to its initial look.
Javascipt Code
The JavaScript controls the state transitions of the button.
Element Selection
The script first grabs references to the button, loader, checkmark, and text.
animateLoading() Function
Triggered immediately after a click.
It:
Displays the loader (display: block).
Sets its width to 24px and starts the infinite rotation animation.
Fades the text slightly (opacity: 0.5) to show that the action is in progress.
animateSuccess() Function
Called after a short delay to represent a completed action.
It:
Hides the loader and removes its rotation animation.
Displays the checkmark, enlarging it for emphasis (36px size).
After 2 seconds, hides the checkmark and restores the text opacity to full (1), resetting the button.
Click Event Listener
When the button is clicked:
It runs animateLoading().
Waits for 1 second (simulating a process such as an API call).
Then runs animateSuccess() to show the checkmark feedback.
Smooth User Experience
The timing (1000ms load, 2000ms success display) ensures users clearly see each phase.
These async animations mimic real-world processes like saving data or submitting forms.
