chenqingspring / react-lottie

Render After Effects animations on React based on lottie-web
MIT License
1.65k stars 363 forks source link

How to trigger animation on hover? #89

Open magtanggol03 opened 5 years ago

magtanggol03 commented 5 years ago

Currently trying to do it using the following but to no avail:

      <Lottie options={defaultOptions}
        height={100}
        width={100}
        isStopped={hover['isStopped']}
        direction={hover['direction']}
        eventListeners={
            [
                { eventName: 'onMouseOver', callback: () => mouseOn() },
            { eventName: 'onMouseLeave', callback: () => mouseOut() }
            ]
        }
      />

Basically goal was to trigger the animation on hover, then reverse the animation when leaving.

goldmont commented 5 years ago
lottie: ?Lottie;
const defaultOptions = {
    loop: true,
    autoplay: false,
    animationData: animationData
};
<div
    onMouseEnter={() => this.lottie && this.lottie.animation.play()}
    onMouseLeave={() => this.lottie && this.lottie.animation.pause()}>
    <Lottie
        ref={(ref) => this.lottie = ref}
        options={defaultOptions}/>
</div>
jeffal commented 4 years ago

@chenqingspring eventListeners prop is not working as indicated by this issue.

sanket-zeus commented 3 years ago

Facing same issue, anyone have solution of it?

rheng001 commented 3 years ago

Same issue, anyone able to trigger animation on hover?

jeffal commented 3 years ago

Here is a screenshot of my code. Similar to goldmont, but I toggle the handleClickToPause function. Screen Shot 2020-11-25 at 3 25 56 PM

rheng001 commented 3 years ago

@jeffal Thanks! I'll be sure to try it out 👍

zeghmouri commented 1 year ago

Hello every one it is working for me, I have used onMouseEnter and onMouseLeave on the parent container.

This is my code :

import React, { useState } from 'react'
import Lottie from 'react-lottie';
import styles from './styles.module.css'

export default function Feature({title,description,lottieData}) {

  const options = {
    loop: false,
    autoplay: false,
    animationData: lottieData,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice"
    }
  };

  const [stopped,setStopped]=useState(true)

  return (
    <div 
      className={styles.feature} 
      onMouseEnter={()=>{setStopped(false)}} 
      onMouseLeave={()=>{setStopped(true)}}
    >
        <Lottie options={options} isClickToPauseDisabled isStopped={stopped} />

        <div>
          <h3>{title}</h3>
          <p>{description}</p>
        </div>
    </div>
  )
}

I hope this will be helpful ^^

AbrarAli14 commented 3 months ago

Hello every one it is working for me, I have used onMouseEnter and onMouseLeave on the parent container.

This is my code :

import React, { useState } from 'react'
import Lottie from 'react-lottie';
import styles from './styles.module.css'

export default function Feature({title,description,lottieData}) {

  const options = {
    loop: false,
    autoplay: false,
    animationData: lottieData,
    rendererSettings: {
      preserveAspectRatio: "xMidYMid slice"
    }
  };

  const [stopped,setStopped]=useState(true)

  return (
    <div 
      className={styles.feature} 
      onMouseEnter={()=>{setStopped(false)}} 
      onMouseLeave={()=>{setStopped(true)}}
    >
        <Lottie options={options} isClickToPauseDisabled isStopped={stopped} />

        <div>
          <h3>{title}</h3>
          <p>{description}</p>
        </div>
    </div>
  )
}

I hope this will be helpful ^^

Nice its working