【问题标题】:Why does a CSS transition on a React component only work in one direction?为什么 React 组件上的 CSS 过渡只在一个方向上起作用?
【发布时间】:2020-05-17 11:04:40
【问题描述】:

我有 4 个 div,我想在按向左或向右箭头键时移动其中一个。这个移动应该是一个过渡而不是瞬时的,但由于某种原因,这个过渡只有在我向左移动时才有效。

我认为这与 key 道具或 div 被卸载有关,但在尝试了一堆不同的东西后我不确定了。

请看:https://codesandbox.io/s/confident-grothendieck-oou90?file=/src/App.js

有人知道怎么回事吗?

【问题讨论】:

    标签: javascript reactjs css-transitions


    【解决方案1】:

    在样式中,您只添加了transition:left 0.3s。所以它不适合正确的。你可以改成transition:all 0.3s

    .tile {
      position: absolute;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 50px;
      height: 50px;
      background-color: lightgrey;
      font-size: 32px;
      transition: all 0.3s ease;
    }
    

    【讨论】:

    • 我没有使用right 来定位图块,只使用left,所以没有理由让它起作用。您可以自己尝试一下,看看它不会改变任何东西。
    猜你喜欢
    • 2021-11-22
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多