我试着用Framer Motion制作一个旋转木马,但是我得到了一个视觉缺陷。以前的组件在新组件之前没有被卸载,我得到了一个奇怪的效果。这个“嘉偶”。
这是处理一切的组件:
const ProjectList = props => {
const [page, setPage] = useState(0);
const projects = [
<Project
name="Example"
desc="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Libero nunc consequat interdum varius sit amet."
image={require("../img/office.jpg")}
/>,
<Project
name="Example2"
desc="Another example. This one does nothing too. What a suprise!"
image={require("../img/office.jpg")}
/>
]
const paginate = (newPage) => {
if(newPage < 0) {
setPage(projects.length - 1)
} else if (newPage > projects.length - 1) {
setPage(0);
} else {
setPage(newPage);
}
}
return (
<div className="project-list">
<AnimatePresence>
<motion.button
key="previous"
onClick={() => paginate(page-1)}
className="carousel-btn"
whileHover={{scale: 1.5, transition: {duration: 0.5}}}
>
<ArrowBackIosIcon/>
</motion.button>
<motion.div
key={page}
initial={{opacity: 0}}
animate={{opacity: 1}}
exit={{opacity: 0}}
>
{projects[page]}
</motion.div>
<motion.button
key="next"
onClick={() => paginate(page+1)}
className="carousel-btn"
whileHover={{scale: 1.5, transition: {duration: 0.5}}}>
<ArrowForwardIosIcon/>
</motion.button>
</AnimatePresence>
</div>
);
};我不知道如何在代码片段工具中使用像framer这样的库,所以下面是CodeSandbox上的简化版本
发布于 2020-08-04 08:56:15
如果您希望一个组件在下一个组件的挂载动画开始之前完成其卸载(exit)动画,则必须打开动画的exitBeforeEnter。
https://www.framer.com/api/motion/animate-presence/#animatepresenceprops.exitbeforeenter
不知道这个视觉错误,因为它在CodeSandbox示例中是不可见的。但这可能是因为组件(暂时)被从DOM中移除,这导致了事情的重新排序。解决方案可能是只将保存图像的motion.div放在AnimatePresence中,就像我在这里所做的那样:
https://stackoverflow.com/questions/63212514
复制相似问题