【问题标题】:How to animate parent width depending on its content?如何根据其内容为父宽度设置动画?
【发布时间】:2020-11-04 22:47:55
【问题描述】:

我有一个模式,它的内容可以在两个不同的组件之间切换。它可以工作,但是过渡很残酷,因为包装器会立即占用其内容的宽度。

如何正确设置动画?我考虑过 transform:scale 但它不起作用。

这里是代码,还有一个沙箱:

import React, {useState} from "react";
import styled from "styled-components"

const Wrapper = styled.div`
background:gainsboro;
width:100%;
height:100%;
border:1px solid lightgrey;
`

const Content1 = () => (
  <div>
    Lorizzle ipsizzle dolor sit amet, ass adipiscing elizzle. Ass 
    izzle velizzle, volutpizzle, suscipit quizzle, we gonna chung 
    vizzle, arcu. Pellentesque egizzle boom shackalack. Fo shizzle 
    my nizzle erizzle. Pimpin' crunk dolor dapibus rizzle tempizzle

    sizzle. Maurizzle fo shizzle mah nizzle fo rizzle, mah home 
    g-dizzle nibh daahng dawg go to hizzle. Shizznit izzle tortor.
     Pellentesque sizzle rhoncizzle shizzlin dizzle. In hizzle 
     habitasse platea dictumst. For sure fo. Break it down izzle 

     urna, pretizzle eu, mattis go to hizzle, eleifend black, nunc.
      Daahng dawg suscipit. Tellivizzle yo mamma velit sed check 
      out this.
    </div>
)

const Content2 = () => (
  <div>
    very short content
    </div>
)

export default function App() {
  const [toggle, setToggle] = useState(false)
  return (
    <Wrapper>
     {toggle ? <Content1 /> : <Content2/>}
     <button onClick={()=> setToggle(!toggle)}>toggle content</button>
    </Wrapper>
  );
}

https://codesandbox.io/s/still-smoke-5ikkg?file=/src/App.js

谢谢!

【问题讨论】:

    标签: css reactjs animation keyframe


    【解决方案1】:

    你可以使用 react-fade-in,不是最好的,而是一个快速的解决方案

    https://www.npmjs.com/package/react-fade-in

    【讨论】:

    • 谢谢,但我不确定淡入淡出是不是很好的解决方案,因为我想为父宽度设置动画。此外,纯 css/js 解决方案会更好。
    猜你喜欢
    • 2019-07-14
    • 2021-09-12
    • 2011-08-06
    • 2019-07-05
    • 1970-01-01
    • 2019-02-28
    • 2021-12-28
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多