【问题标题】:Transition Flexbox As It Stretches With Additional Items使用附加项目拉伸时的过渡 Flexbox
【发布时间】:2022-01-30 01:16:10
【问题描述】:

我想要一个 flexbox(容器,而不是项目)动画/过渡,因为它可以拉伸和收缩以适应其中出现的不同数量的项目。有没有办法让这成为可能?我只能找到为其中的项目大小设置动画的解决方案,而不是 flexbox 本身。

请看下面场景的sn-p:

const { useState } = React

const App = () => {
  const [showSecondDiv, setShowSecondDiv] = useState(true);
  
  return (
    <div>
      <button onClick={() => setShowSecondDiv(!showSecondDiv)}>{(showSecondDiv? "Shrink" : "Expand") + " flexbox"}</button>
      <div className="flex-container">
        <div>Hello</div>
        {showSecondDiv ? <div>World</div> : null}
      </div>
    </div>
  )
}

ReactDOM.render(<App />,
document.getElementById("root"))
.flex-container {
  border-style: solid;
  display: flex;
  flex-direction: column;
  transition: grow 2s;
  max-width: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

<div id="root">
  <!-- This element's contents will be replaced with your component. -->
</div>

【问题讨论】:

  • 我可以通过改变孩子的最大高度来产生你想要的行为,但我认为你不能通过使用 Flexbox 添加一个过渡。您可以添加transition: all 2s 并查看没有产生所需效果的过渡属性。你想让我回答那个实现吗?
  • 抱歉,我不确定我是否理解。通过改变最大高度的实现,你会产生我想要的行为(即,动画 flexbox 本身的收缩/扩展行为)吗?
  • 我添加了答案。

标签: css flexbox css-transitions


【解决方案1】:

在您的情况下,您将无法转换您的 Flexbox

auto 值通常是一个非常复杂的情况。规范 建议不要在 auto 之间设置动画。

据我了解,这也适用于其他不固定的值。

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions#javascript_examples

您可以通过应用 `transition:all 2s' 看到没有过渡属性可以应用您正在寻求的行为

但是,这是我通过动画子元素的最大高度来产生我认为您想要的行为的尝试。

const { useState } = React;

const App = () => {
    const [showSecondDiv, setShowSecondDiv] = useState(true);
    const className = showSecondDiv ? 'grow' : 'shrink';
    return (
        <div>
            <button onClick={() => setShowSecondDiv(!showSecondDiv)}>
                {(showSecondDiv ? 'Shrink' : 'Expand') + ' flexbox'}
            </button>
            <div className="flex-container">
                <div>Hello</div>
                <div className={className}>World</div>
            </div>
        </div>
    );
};

ReactDOM.render(<App />, document.getElementById('root'));
.flex-container {
    border-style: solid;
    display: block;
    flex-direction: column;
    max-width: 100px;
}
.grow {
    animation-name: grow;
}
.shrink {
    animation-name: shrink;
}
.flex-container > div {
    overflow: hidden;
    animation-duration: 2s;
    animation-fill-mode: forwards;
}
@keyframes grow {
    from {
        max-height: 0px;
    }
    to {
        max-height: 1rem;
    }
}
@keyframes shrink {
    from {
        max-height: 1rem;
    }
    to {
        max-height: 0px;
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>

<div id="root">
  <!-- This element's contents will be replaced with your component. -->
</div>

【讨论】:

  • 谢谢。我注意到我们可以在不使用关键帧(因此压缩css)的情况下使用:.grow { max-height: 0px; } .shrink { max-height: 1rem; } 我仍然面临的问题是1rem 值,虽然它适用于示例,但它是硬编码的并且不如果我们向 flexbox 添加更多内容,则可以工作。将其更改为 100% 会使动画效果停止工作。无论单元格中有多少内容,是否有任何其他方法可以使其普遍工作?
  • 无法编辑我上面的评论,但显然类中的值应该被翻转。为错字道歉
猜你喜欢
  • 2018-02-25
  • 1970-01-01
  • 2017-03-13
  • 2018-06-21
  • 2012-07-25
  • 2015-06-19
  • 2013-05-06
  • 1970-01-01
  • 2016-02-01
相关资源
最近更新 更多