首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在用Framer Motion制作旋转木马时有视觉缺陷

在用Framer Motion制作旋转木马时有视觉缺陷
EN

Stack Overflow用户
提问于 2020-08-02 04:55:11
回答 1查看 1.1K关注 0票数 0

我试着用Framer Motion制作一个旋转木马,但是我得到了一个视觉缺陷。以前的组件在新组件之前没有被卸载,我得到了一个奇怪的效果。这个“嘉偶”

这是处理一切的组件:

代码语言:javascript
复制
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上的简化版本

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-04 08:56:15

如果您希望一个组件在下一个组件的挂载动画开始之前完成其卸载(exit)动画,则必须打开动画的exitBeforeEnter

https://www.framer.com/api/motion/animate-presence/#animatepresenceprops.exitbeforeenter

不知道这个视觉错误,因为它在CodeSandbox示例中是不可见的。但这可能是因为组件(暂时)被从DOM中移除,这导致了事情的重新排序。解决方案可能是只将保存图像的motion.div放在AnimatePresence中,就像我在这里所做的那样:

https://codesandbox.io/s/carousel-with-framer-motion-stackoverflow-lmrik?file=/src/components/ProjectList.js

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63212514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档