【发布时间】:2020-10-12 15:30:23
【问题描述】:
我正在开发一个 react 项目,并尝试在 div 出现在视图中后对其进行动画处理和显示。我正在使用“react-intersection-observer”中的 useInView 挂钩来确定 div 是否在视图中,然后启动动画以使 div 可见。
但是,当我在同一个组件中有两个 div 时,这不起作用,即,只要第一个 div 在视图中 - 动画并显示第一个 div,并且当您进一步滚动第二个时div 在视图中 - 动画并显示第二个 div。
任何帮助将不胜感激。
代码如下:
import { motion } from 'framer-motion';
import { useInView } from 'react-intersection-observer';
function Container(props) {
const { ref, inView } = useInView({
threshold: 1
});
const { ref1, inView1 } = useInView({
threshold: 1
});
return (
<div>
<motion.div ref={ref}>
<motion.h1
initial={{ x: -700, opacity: 0 }}
animate={ inView ? { x: 0, opacity: 1 } : ''}
transition={{ duration: .75 }} >
Sample Title 1
</motion.h1>
<motion.p
initial={{ x: 400, opacity: 0 }}
animate={ inView ? { x: 0, opacity: 1 } : ''}
transition={{ delay: 0.8, duration: 1, ease: "easeOut" }} >
Sample Description 1
</motion.p>
</motion.div>
<motion.div ref={ref1}>
<motion.h1
initial={{ opacity: 0 }}
animate={ inView1 ? { opacity: 1 } : ''}
transition={{ duration: .75 }} >
Sample Title 2
</motion.h1>
<motion.p
initial={{ x: 400, opacity: 0 }}
animate={ inView1 ? { x: 0, opacity: 1 } : ''}
transition={{ delay: 0.8, duration: 1, ease: "easeOut" }} >
Sample Description 2
</motion.p>
</motion.div>
</div>
);
}
export default Container;
【问题讨论】:
标签: reactjs intersection-observer framer-motion