【问题标题】:react I want to hide the div tag when the animation is over反应我想在动画结束时隐藏 div 标签
【发布时间】:2021-03-13 08:32:22
【问题描述】:

我正在使用 framer-motion 制作动画。 当动画 pageVariants 完成时,我想隐藏用于动画的 div 标签。 目前,当动画完成时,div 标签被移动到屏幕底部,以便用户可以垂直滚动。我想让它在动画结束后用户不能垂直滚动。 有什么方法可以检测动画何时结束?

import { motion } from 'framer-motion';
import { FunctionComponent } from 'react';

const Home: FunctionComponent = () => {
  const ease = [0.43, 0.13, 0.23, 0.96];

  const pageVariants = {
    initial: {
      y: '0',
      transition: { ease, duration: 10, delay: 0.8 },
    },
    animate: {
      y: '100%',
      transition: { ease, duration: 10 },
      transitionEnd: { display: "none",},
    },
    exit: {
      y: '0',
      opacity: 0,
      transition: { ease, duration: 10, delay: 0.8 },
    },
  };
  return (
    <>
      <div style={{ display: 'grid', gridTemplateColumns: '1fr auto' }}>
        <div className="bg-red-100 h-screen">box</div>
        <div className="bg-blue-100 w-96">box2</div>
      </div>
      <div
        style={{
          width: '300px',
          position: 'absolute',
          marginLeft: 'auto',
          marginRight: 'auto',
          bottom: 0,
          top: 0,
          left: 0,
          right: 0,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
        }}
      >
        <div>
          <img src="profile.svg" />
          <p>hello</p>
        </div>
      </div>
      <motion.div
        className="h-screen bg-white absolute z-50 w-full -top-0"
        variants={pageVariants}
      ></motion.div>
    </>
  );
};

export default Home;

【问题讨论】:

    标签: reactjs typescript framer-motion


    【解决方案1】:

    在 framer-motion 的文档中描述了 transitionEnd 属性。如果你像这样将它设置为你的运动 div,它应该在动画结束时消失:

    <motion.div
            className="h-screen bg-white absolute z-50 w-full -top-0"
            variants={pageVariants}
            animate={{
              transitionEnd: {
                display: "none",
              },
            }}
          >
    </motion.div>
    

    【讨论】:

    • 感谢您的回复。我在 pageVariants 中添加了 transitionEnd 来制作动画。但是,我只能滚动片刻。
    • 如果你能给我提供一个工作示例,例如使用stackblitz.com,我可以检查您为什么不能再滚动了。但是如果没有直观地看到你想要实现的目标,我怀疑我能提供帮助。如果 display:none 不合适,你当然也可以用其他 css 样式来改变它。
    • 在 stackblitz.com 上构建我的环境很困难,而且没有成功。我添加了 display:none,但动画结束后我可以滚动片刻。代码是最新的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多