【问题标题】:React app opacity transition not working in ChromeReact 应用程序不透明度转换在 Chrome 中不起作用
【发布时间】:2018-06-12 10:09:05
【问题描述】:

我正在使用 React 制作番茄钟,我希望消息文本(例如“设置时间”、“焦点”等)和播放/重置按钮在播放/重置时淡出和淡入按钮被按下。因此,按下播放按钮会淡出“设置时间”,然后播放按钮会淡出“焦点”和一旦计时器启动后的重置按钮。

这在 Firefox 中运行良好,但在 Chrome 中淡入无法正常运行,即使我尝试添加 -webkit- 前缀供应商也是如此。我还注意到在 Internet Explorer 中,消息元素在淡入动画后会恢复为 0 不透明度。

您可以通过codesandbox link 查看我的代码和现场演示。

我使用react-transition-group library 将过渡应用到我想在卸载和装载时淡出的元素。

相关的 JSX 在 PomodoroTimer.jsx 的底部,或者这里为了方便:

  <CSSTransitionGroup
    transitionName="fade"
    transitionEnterTimeout={500}
    transitionLeaveTimeout={500}
  >
    {!this.state.timerActive ? 
    <h3 className="message" key="set">Set a time.</h3> :
    (this.state.time < 50) ? 
    <h3 className="message" key="done">Done.</h3> : 
    this.state.timerType == "Rest" ?
    <h3 className="message" key="rest">Rest.</h3> :
    <h3 className="message" key="focus">Focus.</h3>}
  </CSSTransitionGroup>

而相关的CSS在PomodoroTimer.css中,或者这个:

.fade-enter {
  opacity: 0;
}

.fade-enter.fade-enter-active {
  opacity: 1;
  transition: opacity 500ms ease-out;
  transition-delay: 500ms;
}

.fade-leave {
  opacity: 1;
}

.fade-leave.fade-leave-active {
  opacity: 0;
  transition: opacity 500ms ease-in;
}

请帮忙!

【问题讨论】:

  • 它在您提供的沙盒链接中的 chrome 中完美运行。
  • 嗯,这很奇怪,它在我的 Chrome 浏览器中看起来仍然损坏。您能否尝试在 .fade-enter.fade-enter-active { opacity: 1; 中延长过渡持续时间?过渡:不透明度 500ms 缓出;转换延迟:500ms;并告诉我它是否适合您?即使我将转换从 500 毫秒更改为 5000 毫秒,淡入也会迅速而突然地发生,而在 Firefox 中它会正确转换 5000 毫秒。

标签: html css reactjs


【解决方案1】:

这里的问题是您正在更改 css 中的动画时间,如下所示:

.fade-enter {
  opacity: 0;
}

.fade-enter.fade-enter-active {
  opacity: 1;
  transition: opacity 5000ms ease-out;
  transition-delay: 5000ms;
}

.fade-leave {
  opacity: 1;
}

.fade-leave.fade-leave-active {
  opacity: 0;
  transition: opacity 5000ms ease-in;
}

但这里 fade-enter 和 fade-leave 类根据以下代码中定义的时间应用于元素:

<CSSTransitionGroup
   transitionName="fade"
   transitionEnterTimeout={5000}
   transitionLeaveTimeout={5000}
>

上面定义的类在这里适用并根据此处定义的时间被删除。因此,即使您将 css 动画持续时间设置为 5000 毫秒,并且在这里您有 transitionEnterTimeout={500} 和 transitionLeaveTimeout={500}。因此,这些类将在这段时间之后被删除或添加。

因此您在两个地方都保持相同的时间。

【讨论】:

  • 谢谢,您的评论让我在尝试调试应用程序时更加小心。我注意到在 Firefox 中可见过渡持续时间和过渡延迟为 500 毫秒的不透明度过渡,但在 Chrome 中不可见。但是,如果我将转换持续时间延长一点,它在 Chrome 中是可见的。我认为这仅仅是由于浏览器的差异。
  • 是的,通常不同的浏览器在某些情况下会有不同的行为。有很多这样的情况,例如嵌入式文档和 HTML5 视频对于不同的浏览器具有不同的控件和外观。希望这能解决您的问题,如果可以,请接受答案。
猜你喜欢
  • 2015-04-29
  • 2015-06-02
  • 2013-03-14
  • 2013-02-04
  • 1970-01-01
  • 2011-10-02
  • 2018-01-10
  • 2014-10-05
相关资源
最近更新 更多