我正在致力于一个反应项目,并试图动画和显示div一旦他们在视图中。我正在使用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;发布于 2020-10-12 15:48:02
我想你应该把动画和交集事件正确地连接起来。
https://www.framer.com/api/motion/utilities/#animate
InVew const from = useMotionValue(0);
useEffect(
() => {
if (inView) {
const controls = animate(x, 100, {
type: "spring",
stiffness: 2000,
})
return controls.stop
}
},
[inView]
}发布于 2021-07-02 10:06:17
在他们的文档中,它说您可以使用多个变量进行数组析构,而不能使用在您自己的情况下使用的对象析构。
所以你应该这么做。
const [ref, inView ] = useInView()
const [ref2, inView2 ] = useInView()https://stackoverflow.com/questions/64320816
复制相似问题