简体   繁体   English

使用 react.js 滚动动画

[英]Animate on scroll using react.js

I am using some css animations from animate.css and I'm using react.js which works fine at the top of my page however, I also have some animations near the middle of the page.我正在使用animate.css一些 css 动画,我正在使用 react.js,它在我的页面顶部工作正常,但是,我在页面中间附近也有一些动画。 When my page loads everything animates at once which means once I scroll down the animations in the middle of the page have already completed.当我的页面一次加载所有动画时,这意味着一旦我向下滚动页面中间的动画已经完成。 I am looking for away to delay the animations until that area of the screen is visible.我正在寻找延迟动画,直到屏幕的那个区域可见。 I have found some questions/answers on here but they date back quite a few years and appear to be outdated.我在这里找到了一些问题/答案,但它们可以追溯到几年前,似乎已经过时了。

As seen in the code below the animate__animated animate__bounce animate__zoomInDown classes are derived from animate.css but play immediately when the page is loaded and not when visible onscreen:如下面的代码所示, animate__animated animate__bounce animate__zoomInDown类派生自animate.css但在页面加载时立即播放,而不是在屏幕上可见时:

import React from "react";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faHourglassStart} from '@fortawesome/free-solid-svg-icons'



function MiddleContainer() {
  return (
    <div>
    <div id = "middle-container" class="middle-container">
      <h1>What can I offer you?</h1>
      <div className = "fast animate__animated animate__bounce animate__zoomInDown">
      <FontAwesomeIcon className="social-icon" icon={faHourglassStart} size = '4x' color = "black"/>
      <h4>Fast and Reliable Service</h4>
      <p>Your product will be delivered to you with precision, care and in a timely manner.</p>
      <p>Add more info here when you are done with the css. </p>
      </div>
      </div>
      </div>
  )
}

export default MiddleContainer;

So I was able to solve this myself using a different library as I couldn't find any documentation from animate.css on how to animate on scroll所以我能够使用不同的库自己解决这个问题,因为我无法从 animate.css 中找到任何关于如何在滚动上设置动画的文档

The new library with documentation that worked is AOS from https://michalsnik.github.io/aos/带有有效文档的新库是来自https://michalsnik.github.io/aos/ 的AOS

I had to use useEffect from react.js in order for it to work.我必须使用 react.js 中的 useEffect 才能让它工作。

Here is my code with animate on scroll working:这是我的滚动动画代码:

import React, { useEffect } from "react";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faHourglassStart} from '@fortawesome/free-solid-svg-icons'
import AOS from "aos";
import "aos/dist/aos.css";



    function MiddleContainer() {

      useEffect(() => {
        AOS.init({
          // duration : 5000
        });
      }, []);
      return (
    
        
        
        <div>
        <div id = "middle-container" class="middle-container">
          <h1>What can I offer you?</h1>
          <div className = "fast" data-aos="zoom-in">
          <FontAwesomeIcon className="social-icon" icon={faHourglassStart} size = '4x' 
           color = "black"/>
          <h4>Fast and Reliable Service</h4>
          <p>Your product will be delivered to you with precision, care and in a 
           timely manner.</p>
          <p>Add more info here when you are done with the css. </p>
          </div>
    
          
        </div>
    </div>
      )
    }
    
    export default MiddleContainer;

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

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