Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Targeting specific Lottie animations on hover with lottie-web in React

How to target with hover a specific animation when I have multiple lottie animations on the same page in React using lottie-web. lottie-web documentation states that in the lottie.play() I can insert a name parameter to target a specific animation, so I did this lottie.play(container), but it didn't work.

import React, { useEffect, useRef } from "react";
import lottie from "lottie-web";

function App() {
  const containerOne = useRef(null);
  const containerTwo = useRef(null);

  useEffect(() => {
    lottie.loadAnimation({
      container: containerOne.current,
      renderer: "svg",
      loop: true,
      autoplay: false,
      animationData: require("./office.json")
    });

    lottie.loadAnimation({
      container: containerTwo.current,
      renderer: "svg",
      loop: true,
      autoplay: false,
      animationData: require("./office.json")
    });

    return () => {
      lottie.destroy();
    };
  }, []);

  return (
    <div className="App">
      <h1>React Lottie Demo</h1>

      {/* First lottie */}
      <div
        ref={containerOne}
        onMouseEnter={() => lottie.play()}
        onMouseLeave={() => lottie.pause()}
      />

      {/* Second lottie */}
      <div
        ref={containerTwo}
        onMouseEnter={() => lottie.play()}
        onMouseLeave={() => lottie.pause()}
      />
    </div>
  );
}

export default App;

The result on my code is that no matter which animation I hover on both animations will play.

Here is a codesandbox link.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ok I dug deeper while waiting for answers and found that I need to give a name to each animation instead of using the container name like so name: "animationOne" and refer to it later, so my code would look like this:

import React, { useEffect, useRef } from "react";
import lottie from "lottie-web";

function App() {
  const containerOne = useRef(null);
  const containerTwo = useRef(null);

  useEffect(() => {
    lottie.loadAnimation({
      name:"animationOne",
      container: containerOne.current,
      renderer: "svg",
      loop: true,
      autoplay: false,
      animationData: require("./office.json")
    });

    lottie.loadAnimation({
      name:"animationTwo",
      container: containerTwo.current,
      renderer: "svg",
      loop: true,
      autoplay: false,
      animationData: require("./office.json")
    });

    return () => {
      lottie.destroy();
    };
  }, []);

  return (
    <div className="App">
      <h1>React Lottie Demo</h1>

      {/* First lottie */}
      <div
        ref={containerOne}
        onMouseEnter={() => lottie.play("animationOne")}
        onMouseLeave={() => lottie.pause("animationOne")}
      />

      {/* Second lottie */}
      <div
        ref={containerTwo}
        onMouseEnter={() => lottie.play("animationTwo")}
        onMouseLeave={() => lottie.pause("animationOne")}
      />
    </div>
  );
}

export default App;

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!