xxxxxxxxxx
import { motion, useAnimation } from "framer-motion";
import { useInView } from "react-intersection-observer";
import { useEffect } from "react";
const boxVariant = {
visible: { opacity: 1, scale: 1, transition: { duration: 0.5 } },
hidden: { opacity: 0, scale: 0 }
};
const Box = ({ num }) => {
const control = useAnimation();
const [ref, inView] = useInView();
useEffect(() => {
if (inView) {
control.start("visible");
} else {
control.start("hidden");
}
}, [control, inView]);
return (
<motion.div
className="box"
ref={ref}
variants={boxVariant}
initial="hidden"
animate={control}
>
<h1>Box {num} </h1>
</motion.div>
);
};
export default function App() {
return (
<div className="App">
<Box num={1} />
<Box num={2} />
<Box num={3} />
</div>
);
}
xxxxxxxxxx
<ScrollAnimation animateIn='flipInY'
animateOut='flipOutX'>
<h1>
animateOut
</h1>
</ScrollAnimation>
xxxxxxxxxx
import React, { useEffect, useState } from 'react';
import './ScrollAnimation.css';
const ScrollAnimation = () => {
const [showAnimation, setShowAnimation] = useState(false);
useEffect(() => {
const handleScroll = () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const offset = 500; // Adjust this value to determine when the animation should trigger
if (scrollTop > offset) {
setShowAnimation(true);
} else {
setShowAnimation(false);
}
};
window.addEventListener('scroll', handleScroll);
return () => window.removeEventListener('scroll', handleScroll);
}, []);
return (
<div className={`my-element ${showAnimation ? 'animate' : ''}`}>
{/* Your content */}
</div>
);
};
export default ScrollAnimation;