FREE CODE #005 UI Components

Liquid Morphing Toggle Switch Animation

A smooth liquid toggle switch built with HTML, CSS, JavaScript, and SVG. The switch morphs like a soft gel between OFF and ON states with dynamic stretching, reflections, color transitions, accessibility support, and an optional auto-replay mode.

HTML5 CSS3 JavaScript jQuery SVG Web Animations API Web Audio API
VIEWS21
DOWNLOADS13
FORMATZIP
LICENSEFree to use
01 / ABOUT THIS RESOURCE

Use the same code shown in the video.

Liquid Morphing Toggle Switch Animation is a polished frontend microinteraction built with pure HTML, CSS, JavaScript, and SVG.


Instead of sliding a standard circular knob from one side to the other, this component creates a soft liquid-style transition between the OFF and ON states.


When the switch is activated, the gel shape stretches across the track, forms a narrow liquid connection, separates from its starting position, and settles smoothly on the opposite side. The animation gives the toggle a soft, organic feel while keeping the overall interface clean and minimal.


The liquid shape is generated dynamically in JavaScript rather than being a fixed SVG animation. During the transition, the code calculates the changing width, height, tail, neck, stretch, squash, and overshoot of the gel.


The component also redistributes the liquid shape so its overall visual area remains consistent as it stretches and moves. This helps the transition feel more natural instead of simply scaling a circle across the track.


Lighting and reflections move with the liquid shape. Highlights, lower reflections, edge lighting, and a small trailing highlight are updated throughout the animation to create a glossy gel-like appearance.


The surrounding interface also reacts to the state change. The page background and toggle track gradually transition from a cool blue-gray OFF palette into a softer green ON palette.


The OFF and ON labels fade between states so the selected state remains clear without interrupting the animation.


Rapid user input is handled safely. If the switch is clicked again while an animation is already running, the requested destination is updated and the current transition completes before moving toward the latest requested state.


An optional Auto Replay mode is included for presentations, portfolio demos, social media recordings, and UI showcases. When enabled, the toggle automatically switches between ON and OFF states.


The component uses a real button with switch semantics and updates aria-checked whenever the requested state changes. It also provides screen-reader status updates and visible keyboard focus styling.


Users who enable reduced-motion preferences receive the final state immediately instead of being forced through the full liquid animation.


Everything is contained in a single standalone HTML file. No external CSS framework, JavaScript library, icon package, image file, or animation library is required.


Key Features


• Liquid morphing toggle animation

• Smooth OFF-to-ON transition

• Dynamic SVG shape generation

• Stretch and squash effects

• Liquid neck and trailing-drop behavior

• Organic overshoot motion

• Glossy highlights and reflections

• Dynamic color transitions

• Animated OFF and ON labels

• Responsive layout

• Auto Replay mode

• Rapid-click state handling

• Accessible switch semantics

• ARIA checked-state updates

• Screen-reader status feedback

• Keyboard-friendly button interaction

• Focus-visible styling

• Reduced-motion support

• Pure HTML, CSS, JavaScript, and SVG

• No external libraries

• No external image assets


How It Works


The toggle keeps track of its current state, requested destination, and active animation.


When the user clicks the switch, JavaScript requests the opposite state and starts a timed animation.


Instead of moving a fixed circle, the code generates a new liquid contour for every animation frame.


Several values control the shape throughout the transition:


• Anticipation slightly compresses the gel before movement

• Stretch expands the liquid horizontally during travel

• Squash changes the shape as it approaches the destination

• Overshoot moves the gel slightly beyond its resting point before settling

• Tail calculations preserve part of the original mass while the main body travels

• Neck calculations create the thin liquid connection between the moving and trailing portions


The liquid contour is sampled across many horizontal positions and converted into a smooth SVG path using Catmull–Rom style curve calculations.


The code then estimates the area of the generated shape and vertically rescales it so the liquid keeps approximately the same overall area during the morph.


This creates a more believable liquid effect than simply changing width and height with CSS.


Visual Details


The liquid uses layered SVG gradients, edge highlights, reflections, shadows, and clipped lighting effects.


As the gel moves, the main reflection follows its current position and dimensions.


The background and track colors also interpolate between the OFF and ON palettes, making the entire component feel connected to the switch state.


Interaction Behavior


The component supports repeated clicks without running multiple overlapping animations.


If the user changes the requested state during an active transition, the current sequence finishes and the component automatically moves toward the latest requested state.


This keeps the animation stable even when the button is clicked quickly.


Auto Replay


The Auto Replay option automatically alternates between OFF and ON states.


This is useful for:


• Portfolio demonstrations

• UI showcases

• Social media recordings

• Landing-page previews

• Animation experiments


The automatic sequence pauses naturally while another animation is active and resumes after the component settles.


Accessibility


The toggle uses role="switch" and updates aria-checked between false and true.


A live status region announces the final state as Off or On.


The button also includes focus-visible styling for keyboard users.


The component detects prefers-reduced-motion and skips the full animated transition when reduced motion is requested.


Customization


The component can be customized without changing its overall architecture.


You can adjust:


• Page background colors

• Track colors

• Gel colors

• ON and OFF palettes

• Toggle dimensions

• Animation duration

• Gel radius

• Stretch amount

• Squash amount

• Overshoot strength

• Reflection intensity

• Shadow softness

• Auto Replay timing


The same liquid-morphing technique can also be adapted for buttons, sliders, tab selectors, loading states, filters, settings controls, or other interactive UI elements.


Recommended Use Cases


• Settings screens

• Dashboard controls

• Mobile-style interfaces

• SaaS applications

• Smart-home controls

• Creative toggle switches

• Portfolio projects

• UI/UX inspiration

• Frontend animation experiments

• SVG animation practice

• Interactive design showcases


Important Note


This component provides the visual and interaction behavior of a toggle switch.


It does not save the selected state to a database, local storage, API, or backend service by itself.


For a production application, connect the switch state to your own settings logic, application state, API endpoint, or persistence layer.

READY TO BUILD?

Download the code and customize it.

No account is required. Downloads pass through Ahmad Khan Codes so the resource can keep an accurate download count.

Download ZIP