简体   繁体   English

决定 CSS 过渡的顺序

[英]Decide order of CSS Transitions

Funky problem i'm having, although i bet theres a slick way to solve it.我遇到了一个奇怪的问题,尽管我打赌有一个巧妙的方法来解决它。

The circumstance is that i have a table that has three column sort states: completely unsorted, in which case i dont want any icon present, sorted ascending, in which i want an up arrow, and sorted descending, in which i want a down arrow.情况是我有一个表,它具有三列排序状态:完全未排序,在这种情况下我不想要任何图标,升序排序,我想要一个向上箭头,降序排序,我想要一个向下箭头.

clicking the column header should take you through these three states.单击列标题应该会带您完成这三种状态。

starts off => click => ascending => click => descending => click => back to off

This all works fine and dandy, except for the fact that i want to use the same Caret element, and then use css transition's to fade it in from opacity: 0 to opacity: 1 , then on click rotate it 180deg to show a down arrow for descending, then finally when clicked again to remove the sort, i want it to fade out WITHOUT ROTATING BACK TO 0 DEG除了我想使用相同的 Caret 元素,然后使用css 过渡将其从opacity: 0淡入到opacity: 1 ,然后单击rotate it 180deg以显示向下箭头rotate it 180degrotate it 180deg降序,然后最后再次单击以删除排序时,我希望它淡出而不旋转回 0 度

That last part is where the problem lies.最后一部分是问题所在。

EDIT编辑

I have only recreated the behavior in the sandbox, but i am really using react-table , so there are only three possible states since it is controlled by the package:我只在沙箱中重新创建了行为,但我确实在使用react-table ,所以只有三种可能的状态,因为它是由包控制的:

initial state: {showCaret: false, flipped: false}
first click: {showCaret: true, flipped: false}
second click: {showCaret: true, flipped: true}
third click, back to initial: {showCaret: false, flipped: false}

the state changes are controlled by react-table , so i cant setTimeout on the flipped variable.状态变化由react-table控制,所以我不能在翻转变量上设置超时。

I am looking for a purely CSS way to achieve this goal without manipulating the way the state changes, if possible如果可能的话,我正在寻找一种纯粹的 CSS 方式来实现这一目标,而无需操纵状态变化的方式

END EDIT结束编辑

I've attached a codesandbox to demonstrate.我附上了一个代码沙盒来演示。 First click Show Caret, then Flip Caret, then Hide Caret.首先单击“显示插入符”,然后单击“翻转插入符”,然后单击“隐藏插入符”。 The css is set up basically the same as mine is currently in my actual project too. css的设置与我目前在我的实际项目中的设置基本相同。

https://codesandbox.io/embed/admiring-rain-svsc9?fontsize=14&hidenavigation=1&theme=dark https://codesandbox.io/embed/admiring-rain-svsc9?fontsize=14&hidenavigation=1&theme=dark

It sounds like what you want is for the arrow to disappear, but not to rotate back to its starting orientation as it disappears.听起来您想要的是箭头消失,但不要在它消失时旋转回其起始方向。

Since you are handling and tracking all this with React state, you could just set those two states separately, timed .3s apart (since that is your CSS transition time).由于您使用 React 状态处理和跟踪所有这些,您可以单独设置这两个状态,时间间隔 0.3 秒(因为这是您的 CSS 转换时间)。

You could do this in a number of ways.您可以通过多种方式做到这一点。 To demonstrate it, in this fork of your example I have you just setting the on/off visibility to off, and then separately, in componentDidUpdate, I have it watching for whenever it's turned off, at which point it waits 300ms (.3s) and then sets the rotate state back.为了演示它,在您示例的这个分支中,我让您将开/关可见性设置为关闭,然后分别在 componentDidUpdate 中,我让它在关闭时监视它,此时它等待 300 毫秒(.3 秒)然后将旋转状态设置回来。

componentDidUpdate(oldProps, oldState) {
    if (oldState.showCaret && !this.state.showCaret) {
      //it was just hidden
      setTimeout(() => {
        this.setState({
          flipped: false
        });
      }, 300);
    }
}

https://codesandbox.io/s/sparkling-pine-9igec https://codesandbox.io/s/sparkling-pine-9igec

EDIT, with CSS only solution编辑,仅使用 CSS 解决方案

https://codesandbox.io/s/pensive-wilbur-opxzf https://codesandbox.io/s/pensive-wilbur-opxzf

/* flipped taking care of rotating the img tag */
.image {
  transition: transform 0.3s linear 2s;
}
.flipped {
  transform: rotate(180deg);
  transition: transform 0.3s;
}

Change改变

onClick={() => this.setState({ showCaret: false, flipped: false })}

To

onClick={() => this.setState({ showCaret: false })}

and it should work.它应该工作。

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

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