1 reactjs intersection-observer framer-motion
我正在开发一个 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;
Run Code Online (Sandbox Code Playgroud)
在他们的文档中,它说您可以将多个变量与数组解构一起使用,而不是在您自己的情况下使用的对象解构。
所以你应该这样做。
const [ref, inView ] = useInView()
const [ref2, inView2 ] = useInView()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10174 次 |
| 最近记录: |