简体   繁体   English

React + Lottie Animation 滚动火

[英]React + Lottie Animation scroll fire

I created different animation thought Bodymovin.我创建了不同的 animation 认为 Bodymovin。 I would like to fire the animation once the scroll reach the animation.一旦滚动到达 animation,我想触发 animation。 I am trying to figure out the way, but unfortunately I am not able to do it.我正在尝试找出方法,但不幸的是我无法做到。

I am working with Gatsby JS (React framework).我正在使用 Gatsby JS(React 框架)。 I paste my code down here of the.js file:我将代码粘贴到 .js 文件的此处:

import React, { Component } from 'react'
import Lottie from 'react-lottie'
import animationData from './paginaweb.json'

class UncontrolledLottie extends Component {


  render(){

    const defaultOptions = {
      loop: false,
      autoplay: true, 
      animationData: animationData,
      rendererSettings: {
        preserveAspectRatio: 'xMidYMid slice'
      }
    };

    return(
      <div>
        <Lottie options={defaultOptions}
              width={200}
        />
      </div>
    )
  }
}

export default UncontrolledLottie

I tried libraries as https://www.npmjs.com/package/react-animate-on-scroll but didn't work to me.我尝试了库为https://www.npmjs.com/package/react-animate-on-scroll但对我不起作用。 The animation shows up but it's already fired. animation 出现但它已经被解雇了。

Someone out there that can help me out with this?有人可以帮我解决这个问题吗? I will really appreciate.我会很感激的。

Thank you谢谢

I done similar things with react-waypont.我用 react-waypont 做了类似的事情。 You can place a waypoint somewhere in the page.您可以在页面的某处放置一个航点。 If your scroll reach it, then you can trigger something.如果您的滚动到达它,那么您可以触发某些东西。 Make sure you just render the animation when this event triggered, so it just starts when you reach there.确保在触发此事件时仅渲染 animation,因此它只会在您到达那里时开始。 Something like this.像这样的东西。

import {Waypoint} from 'react-waypoint';
import React, {useState} from 'react';

const Component = () => {
let [renderLottie, set] = useState(false);

return (
   ...
    components to push waypoint out of the first page, so you can scroll to it
   ...
    <Waypoint
      onEnter={set(true)}
    />
    {renderLottie && UncontrolledLottie}
  )
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM