【问题标题】:Add Animation/Transition to Element When Removed From DOM从 DOM 中删除时向元素添加动画/过渡
【发布时间】:2019-08-19 03:59:40
【问题描述】:

我有一个用于错误消息元素 ErrorMessage 的 React 组件,它要么返回带有子元素的 div 元素,要么返回 null,因此它会从 DOM 中删除。

我很好奇 CSS/React 是否有办法在元素从 DOM 中移除时对其进行动画处理?我可以在渲染良好时制作动画。

这是一个CodeSandBox 来测试它。

这是我的 SASS

.ErrorMessages {
  background: #ee4900;
  color: #fff;
  border: 0;
  color: #fff;
  padding: 0.8em 1em;
  text-align: left;
  font-size: 12px;
  margin-top: 10px;
  margin-bottom: 10px;
  animation-duration: 0.5s;
  animation-fill-mode: both;
  animation-name: fadeInUp;
}

.ErrorMessages--Centered {
  padding-top: 30px;
  padding-right: 70px;
  padding-left: 70px;
}

.ErrorMessages--fadeOut {
  animation-name: fadeOutDown;
}

@keyframes fadeOutDown {
  from {
    opacity: 1;
    max-height: 72px;
  }

  to {
    opacity: 0;
    padding: 0;
    margin: 0;
    max-height: 0;
    transform: translate3d(0, 50px, 0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    padding: 0;
    margin: 0;
    max-height: 0;
    transform: translate3d(0, 50px, 0);
  }
  to {
    opacity: 1;
  }
}

下面是我的 ErrorMessage 组件

import React from "react";

const ErrorMessage = props => {
  let err = props.show ? (
    <div className="ErrorMessages ErrorMessages--Centered">
      <h4>{props.errorHeader}</h4>
      <p>{props.errorMessage}</p>
    </div>
  ) : null;
  return err;
};

export default ErrorMessage;

在 DOM 中呈现为

<div class="ErrorMessages ErrorMessages--Centered"></div>

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript css reactjs sass css-animations


    【解决方案1】:

    如果不延迟组件卸载,您将无法为其设置动画 - 否则从技术上讲该组件不再存在,因此您无法在其离开时添加任何动画/过渡。

    我个人认为 react-transition-group 处理动画非常好。

    https://github.com/reactjs/react-transition-group

    然后你可以使用类似的东西:

    <TransitionGroup>
        {this.state.showError && 
        <CSSTransition className='animate-error' timeout={{enter: 500, exit: 500}}>
            <ErrorMesage ... />
        </CSSTransition>
        }
    </TransitionGroup>
    

    并控制如下状态:

    .animate-error-enter .ErrorMessages.ErrorMessages--Centered { 
        the styles for the initial state of your animation/transition before it enters
    }
    
    .animate-error-enter-done .ErrorMessages.ErrorMessages--Centered { 
        the styles you animate to on enter 
    }
    
    .animate-error-exit .ErrorMessages.ErrorMessage--Centered { 
        the styles you animate to on exit in order to hide the component 
    }
    

    超时进入和超时退出应该等于进入完成和退出的动画/过渡持续时间。

    【讨论】:

      【解决方案2】:

      您可以setTimeout 并在props.show 更改时引发动画标志。像这样的:

      const ErrorMessage = props => {
        const [show, setShow] = useState(props.show);
        const [hide, setHide] = useState(!props.show);
        const timeoutRef = useRef(null);
      
        useEffect(() => {
          if (props.show) {
            setShow(true);
            setHide(false);
            clearTimeout(timeoutRef.current);
          }
          else {
            setShow(false);
            timeoutRef.current = setTimeout(() => setHide(true), props.delay || 1000);
          }
        }, [props.show]);
      
        /* unmount cleanup */
        useEffect(() => () => clearTimeout(timeoutRef.current), []);
      
        let err = !hide ? (
          <div className={'ErrorMessages ErrorMessages--Centered' + (show ? '' : ' ErrorMessages--fadeOut')}>
            <h4>{props.errorHeader}</h4>
            <p>{props.errorMessage}</p>
          </div>
        ) : null;
        return err;
      };
      

      【讨论】:

      • 太完美了,谢谢!像魅力一样工作,没想过使用超时!
      猜你喜欢
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 2017-07-19
      • 1970-01-01
      相关资源
      最近更新 更多