简体   繁体   English

CSS转换,绝对定位和React Transition Group的问题

[英]Trouble with CSS transition, absolute positioning, and React Transition Group

I'm working on some basic FreeCodeCamp challenges, and I'm trying to use React Transition Group to achieve some pretty simple animations. 我正在研究一些基本的FreeCodeCamp挑战,我正在尝试使用React Transition Group来实现一些非常简单的动画。 Here's the CodePen . 这是CodePen

What I've run in to is that I can't find out how to have the "quote card" horizontally and vertically centered, and also have my animations (that I'm using React Transition Group to trigger) perform a translation on it. 我遇到的是我无法找到如何将“报价卡”水平和垂直居中,并且还有我的动画(我正在使用React Transition Group触发)对其执行翻译。 I have the element that I'm trying to move ( #quote-box ) centered with the following css: 我有一个我想要移动的元素( #quote-box )以下面的css为中心:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Here're the "move" CSS classes that I'm using with the Transition Group: 以下是我与Transition Group一起使用的“移动”CSS类:

.move-enter {
  opacity: 0.01;
  transform: translate(-200px, 0);
}

.move-enter-active {
  opacity: 1;
  transform: translate(0, 0);
  transition: all 500ms ease-in 200ms;
}

.move-exit {
  opacity: 1;
  transform: translate(0, 0);
}

.move-exit-active {
  opacity: 0.01;
  transform: translate(200px, 0);
  transition: all 500ms ease-in 200ms;
}

I assume that I should be setting the transition properties to be left instead of all , but I'm a little lost in what is preventing the movement from happening. 我认为我应该设置transition性质被left ,而不是all ,但我在什么是防止发生运动有点失落。 Should I also have a move-exited and move-entered class with the appropriate positioning? 我是否还应该使用适当的定位进行move-exitedmove-entered课程? Thanks in advance! 提前致谢!

There is a pretty major hint as to what might be going wrong here, and that is the fact that in your CodePen, the cards successfully animate their opacity but not their transform . 关于这里可能出现什么问题,有一个非常重要的暗示,那就是在你的CodePen中,这些卡成功地激活了它们的opacity而不是它们的transform That tells you that something is working as expected, just not all of the transition. 这告诉你某些东西按预期工作,而不是所有的过渡。

So, our first step would be to investigate the transition as it happens. 因此,我们的第一步是调查转变。 Indeed, if we slow down the CSS transition duration to 20 seconds and set the <ReactTransitionGroup.CSSTransition> timeout also to 20 seconds, and inspect the element div#quote-box with devtools as the transition is happening, we see something fishy: 实际上,如果我们将CSS转换持续时间减慢到20秒并将<ReactTransitionGroup.CSSTransition>超时也设置为20秒,并在转换发生时使用devtools检查元素div#quote-box ,我们会看到一些可疑的东西:

在此输入图像描述

Your CSS transform property for #quote-box is overriding the transition group transform property for .move-enter-active (as expected - see CSS selector specificity ) (you can tell because the transform property in .move-enter-active is crossed out). #quote-box CSS转换属性覆盖了.move-enter-active的转换组转换属性(正如预期的那样 - 请参阅CSS选择器特性 )(您可以判断,因为.move-enter-activetransform属性已被删除)。 In other words, your CSS transition group transform is never applied, but the opacity transition is unaffected because #quote-box does not set that property. 换句话说,从不应用CSS过渡组变换,但opacity过渡不受影响,因为#quote-box不设置该属性。

There are a few potential solutions here. 这里有一些潜在的解决方案。 One of the most simple involves two steps: 最简单的一个涉及两个步骤:

  1. Change #quote-box from an ID to a class ( .quote-box ) - (you should absolutely do this anyway, and also for any IDs on the page, because you should only ever have a single instance of the same named ID on the page, and with CSS transition group you will have at least two at some points.) This will also ensure that your CSS transition group .move-* selectors will have the proper priority. #quote-box从ID更改为类( .quote-box ) - (无论如何,您应该绝对这样做,也应该对页面上的任何ID进行更改,因为您应该只有一个具有相同命名ID的实例)在页面上,使用CSS过渡组,您将在某些点上至少有两个。)这也将确保您的CSS过渡组.move-*选择器具有适当的优先级。
// CSS
.quote-box {
  // etc
}

// JSX
<div className="quote-box" >
  {/* etc */}
</div>
  1. Adjust your CSS to use the calc() function to calculate the position of the quote box. 调整CSS以使用calc()函数计算报价框的位置。 This is because normally you would not be able to center the quote box with transform(-50%, -50%) and offset its position for the transition with transform(-200px, 0) at the same time. 这是因为通常你不会将能够与中心报价框中transform(-50%, -50%) 抵消其位置与过渡transform(-200px, 0)在同一时间。 To do so, we must use calc() to combine both the centering transform and the transition offset at the same time, ie transform: translate(calc(-50% - 200px), -50%); 要做到这一点,我们必须使用calc()结合两个中心转变过渡的同时偏移,即transform: translate(calc(-50% - 200px), -50%); :
.move-enter {
  opacity: 0.01;
  transform: translate(calc(-50% - 200px), -50%);
}

.move-enter-active {
  opacity: 1;
  transform: translate(-50%, -50%);
  transition: all 500ms ease-in 200ms;
}

.move-exit {
  opacity: 1;
  transform: translate(-50%, -50%);
}

.move-exit-active {
  opacity: 0.01;
  transform: translate(calc(-50% + 200px), -50%);
  transition: all 500ms ease-in 200ms;
}

https://codepen.io/_jered/pen/KKPomVK https://codepen.io/_jered/pen/KKPomVK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM