webdevpuneet.com

Lottie Hover Icon Button — Free Inline-JSON Lottie Snippet

Lottie Hover Icon Button · Buttons · Plain HTML, CSS & JS · Live preview

CategoryButtons

Copy & paste

Source Code

Requires
<div class="lb-wrap">
  <button class="lb-btn" id="lbBtn">
    <span class="lb-icon" id="lbIcon"></span>
    <span class="lb-label">Add to favorites</span>
  </button>
  <p class="lb-hint">Hover the button — the icon plays a Lottie animation.</p>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Inline animationData
No hosted .json file, no network request.
Play-on-hover pattern
mouseenter/mouseleave drive play() calls.
Smooth reverse
setDirection(-1) reverses from the current frame.
Non-looping icon
Settles at a clear off/on state, not a busy loop.
Real Lottie structure
A genuine minimal keyframed JSON document.
SVG rendering
Crisp at any size, no raster scaling artifacts.
Single dependency
Only lottie-web is required, no plugins.
Drop-in swap
Replace animationData with any exported file's JSON.

About this UI Snippet

Lottie Hover Icon Button — Play/Reverse on Hover With Inline Animation Data

Screenshot of the Lottie Hover Icon Button snippet rendered live

Lottie plays back animations exported from After Effects (via the Bodymovin plugin) or built directly as JSON, and lottie-web is the runtime that renders that JSON to SVG, canvas, or HTML in the browser. The classic use is a button icon that plays a short flourish on hover — a heart filling in, a bell ringing, a star sparkling — driven by lottie.loadAnimation with autoplay: false and play()/setDirection() calls on mouseenter/mouseleave. This snippet builds exactly that, but since a live sandbox has no server to host a .json asset on, the Lottie data is a small hand-built object passed directly as animationData, so the button always works standalone with zero network requests.

A real, minimal Lottie document

heartAnimation is a genuine Lottie JSON structure — v (format version), fr/ip/op (frame rate and in/out points), and a single shape layer with keyframed s (scale) and a fill c (color) that shift over 30 frames. It's deliberately small (one layer, two keyframed properties) but it's not a mock — lottie.loadAnimation parses and renders it exactly as it would a much larger exported file, which is why this pattern is worth knowing: any time you need a Lottie animation that must never depend on an external asset load, hand-authoring or generating a minimal JSON document and passing it via animationData is the way to guarantee that.

Play forward, reverse back

autoplay: false means nothing happens until you interact. On mouseenter, setDirection(1) then play() runs the animation forward from wherever it currently is; on mouseleave, setDirection(-1) then play() runs it backward to the start. Because Lottie tracks the current frame internally, rapidly hovering on and off reverses smoothly from the interrupted frame rather than snapping or replaying from zero.

Why not autoplay + loop?

A looping hover icon can feel busy on a button that's meant to communicate a single action. This snippet uses loop: false with the play/reverse pattern so the icon settles at a clear "off" state and a clear "hovered" state — a better fit for buttons than for a standalone decorative animation like a loading indicator.

Where this fits

For the equivalent asset-independence pattern applied to a canvas-rendered Rive state machine instead of an SVG Lottie, see Rive interactive icon. For a CSS-only celebratory micro-interaction with no animation library at all, see confetti button or interactive hover button.

Customizing it

Export your own icon animation from After Effects with Bodymovin (or build one in LottieFiles' editor) and either host the resulting .json and pass a path instead of animationData, or inline it the same way this snippet does if you need zero-network guarantees. Swap renderer: 'svg' for 'canvas' if you're animating many icons at once and want lower per-instance overhead.

Step by step

How to Use

  1. 1
    Add the lottie-web CDNInclude lottie.min.js from the CDN panel.
  2. 2
    Paste HTML, CSS, and JSA button with an empty icon container renders.
  3. 3
    Hover the buttonThe heart icon animates in, scaling and changing color.
  4. 4
    Move awayThe animation reverses smoothly back to its start frame.
  5. 5
    Swap the animation dataReplace heartAnimation with your exported Lottie JSON.
  6. 6
    Use a hosted file insteadPass path: 'your.json' in place of animationData.

Real-world uses

Common Use Cases

Favourite and like buttons
Play a heart or star animation on hover, using mouseenter and mouseleave to drive play() calls on a small inline animation.
Notification actions
Pair with a notification bell so that a bell icon animates when the pointer reaches the button.
Add-to-cart flourishes
Add an icon flourish alongside an add to cart button, with setDirection(-1) reversing smoothly from the current frame.
Navigation icon buttons
Give subtle hover motion to icon-only navigation, using non-looping animations that settle at a clear off or on state.
Asset-independent micro-interactions
Reuse beside interactive hover button or the Rive interactive icon, since inline animationData avoids hosted JSON files and extra network requests.
Related: Shake to Undo
See the Shake to Undo for a related buttons pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

A live code sandbox has no server to host a .json asset on, and referencing an external URL risks a broken icon if that file ever moves or 404s. Passing a hand-built object directly as animationData sidesteps both problems entirely — lottie-web accepts a JavaScript object just as readily as a fetched JSON file, so the button always renders correctly with zero network dependency.

It's a genuine, if minimal, Lottie JSON document — it has the required top-level fields (v, fr, ip, op, layers) and a shape layer with real keyframed scale and color properties. lottie-web parses and renders it exactly as it would a much larger file exported from After Effects; it's just deliberately small, with one layer and two animated properties, rather than a mock object with fake-looking data.

autoplay is set to false so nothing plays until interaction. On mouseenter, setDirection(1) then play() runs the animation forward from its current frame; on mouseleave, setDirection(-1) then play() runs it backward toward frame 0. Because Lottie tracks the current frame rather than always restarting, moving the mouse on and off quickly reverses smoothly from wherever playback was interrupted instead of jumping or restarting from scratch.

Export a Bodymovin/Lottie JSON from After Effects (or build one in the LottieFiles editor), then either host that .json file and pass path: "your-icon.json" instead of animationData in the loadAnimation call, or — if you need the same zero-network guarantee this snippet has — inline the exported JSON object directly as animationData the same way heartAnimation is defined here.

renderer: "svg" produces crisp vector output at any button size with no raster scaling artifacts, and is the more common default for a single small icon animation. renderer: "canvas" trades that crispness for lower per-instance overhead, which matters more when animating many Lottie icons simultaneously on one page — for a single hover icon like this button, SVG is the better default.