Transitions在CSS React.js中不起作用。

huangapple go评论100阅读模式
英文:

Transitions isn't working in css React.js

问题

I have a css property transition set to height 3s. However, it isn't working. I'm on react.js

Short part of code. Jsx:

  1. function Form({showPopup}) {
  2. const [height, setHeight] = useState("400px")
  3. function proceedWithOtp(message){
  4. setHeight("300px")
  5. setForm(<OtpForm message={message}/>)
  6. }
  7. const [form, setForm] = useState(<RegisterForm showPopup={showPopup} proceedWithOtp={proceedWithOtp}/>)
  8. return (
  9. // This system is used so that height animation can work
  10. <div className="register-form" style={{
  11. height: height
  12. }}>
  13. {form}
  14. </div>
  15. )
  16. }

Css:

  1. .Register .register-form form {
  2. width: 350px;
  3. padding: 25px;
  4. border-radius: 25px;
  5. }
  6. .Register .register-form {
  7. display: flex;
  8. justify-content: center;
  9. align-items: center;
  10. transition: height 5s;
  11. }
  12. .Register .register-form form * {
  13. padding: 5px;
  14. }

This is what RegisterForm returns:

  1. <form onSubmit={handleSubmit} className="bg-white text-dark email-form">
  2. <h1 className="text-center font-hagrid">Register</h1>
  3. <br />
  4. <div className="mb-3">
  5. <input type="text" value={name} onChange={e => setName(e.target.value)} required placeholder="Enter your Name" className="form-control" />
  6. </div>
  7. <div className="mb-3">
  8. <input type="email" value={email} onChange={e => setEmail(e.target.value)} required placeholder="Enter your email address" className="form-control" />
  9. </div>
  10. <div className="mb-3">
  11. <input placeholder="Enter your password" required value={password} onChange={e => setPassword(e.target.value)} type="password" className="form-control" />
  12. </div>
  13. <ReCAPTCHA
  14. sitekey={RECAPTCHA_SITE_KEY}
  15. size="invisible"
  16. ref={recaptchaRef}
  17. />
  18. <button disabled={buttonDisabled} type="submit" className="btn btn-primary btn-submit text-dark">{buttonContent}</button>
  19. </form>

OtpForm returns:

  1. <form className="bg-white text-dark otp-form" >
  2. <p className="text-center">{message}</p>
  3. <div className="input_field_box mb-3">
  4. <input ref={ref1} name="1" onChange={otpBoxChange} type="number" />
  5. <input ref={ref2} name="2" onChange={otpBoxChange} type="number" disabled />
  6. <input ref={ref3} name="3" onChange={otpBoxChange} type="number" disabled />
  7. <input ref={ref4} name="4" onChange={otpBoxChange} type="number" disabled />
  8. </div>
  9. <button disabled={false} type="submit" className="btn btn-primary btn-submit text-dark">{"Submit"}</button>
  10. </form>

This is a form for register. The items are centered with a definite width. The height is fixed and I want it to animate. However, for some reason, it's not animating with the transition property in CSS. How do I fix this now? I'm unable to figure it out.

英文:

I have a css property transition set to height 3s. However, it isn't working. I m on react.js

When I try to do it, there is no transition on height.

Short part of code. Jsx:

  1. function Form({showPopup}){
  2. const [height, setHeight] = useState(&quot;400px&quot;)
  3. function proceedWithOtp(message){
  4. setHeight(&quot;300px&quot;)
  5. setForm(&lt;OtpForm message={message}/&gt;)
  6. }
  7. const [form, setForm] = useState(&lt;RegisterForm showPopup={showPopup} proceedWithOtp={proceedWithOtp}/&gt;)
  8. return (
  9. // This system is used so that height animation can work
  10. &lt;div className=&quot;register-form&quot; style={{
  11. height: height
  12. }}&gt;
  13. {form}
  14. &lt;/div&gt;
  15. )
  16. }

Css:

  1. .Register .register-form form {
  2. width: 350px;
  3. padding: 25px;
  4. border-radius: 25px;
  5. }
  6. .Register .register-form {
  7. display: flex;
  8. justify-content: center;
  9. align-items: center;
  10. transition: height 5s;
  11. }
  12. .Register .register-form form * {
  13. padding: 5px;
  14. }

This is what RegisterForm returns:

  1. &lt;form onSubmit={handleSubmit} className=&quot;bg-white text-dark email-form&quot;&gt;
  2. &lt;h1 className=&quot;text-center font-hagrid&quot;&gt;Register&lt;/h1&gt;
  3. &lt;br /&gt;
  4. &lt;div className=&quot;mb-3&quot;&gt;
  5. &lt;input type=&quot;text&quot; value={name} onChange={e =&gt; setName(e.target.value)} required placeholder=&quot;Enter your Name&quot; className=&quot;form-control&quot; /&gt;
  6. &lt;/div&gt;
  7. &lt;div className=&quot;mb-3&quot;&gt;
  8. &lt;input type=&quot;email&quot;value={email} onChange={e =&gt; setEmail(e.target.value)} required placeholder=&quot;Enter your email address&quot; className=&quot;form-control&quot; /&gt;
  9. {/* &lt;div id=&quot;emailHelp&quot; className=&quot;form-text&quot;&gt;Your passwords are 100% secure and encrypted with us!&lt;/div&gt; */}
  10. &lt;/div&gt;
  11. &lt;div className=&quot;mb-3&quot;&gt;
  12. &lt;input placeholder=&quot;Enter your password&quot; required value={password} onChange={e =&gt; setPassword(e.target.value)}type=&quot;password&quot; className=&quot;form-control&quot; /&gt;
  13. &lt;/div&gt;
  14. &lt;ReCAPTCHA
  15. sitekey={RECAPTCHA_SITE_KEY}
  16. size=&quot;invisible&quot;
  17. ref={recaptchaRef}
  18. /&gt;
  19. &lt;button disabled={buttonDisabled} type=&quot;submit&quot; className=&quot;btn btn-primary btn-submit text-dark&quot;&gt;{buttonContent}&lt;/button&gt;
  20. &lt;/form&gt;

OtpForm returns:

  1. &lt;form className=&quot;bg-white text-dark otp-form&quot; &gt;
  2. &lt;p className=&quot;text-center&quot;&gt;{message}&lt;/p&gt;
  3. &lt;div className=&quot;input_field_box mb-3&quot;&gt;
  4. &lt;input ref={ref1} name=&quot;1&quot; onChange={otpBoxChange} type=&quot;number&quot; /&gt;
  5. &lt;input ref={ref2} name=&quot;2&quot; onChange={otpBoxChange} type=&quot;number&quot; disabled /&gt;
  6. &lt;input ref={ref3} name=&quot;3&quot; onChange={otpBoxChange} type=&quot;number&quot; disabled /&gt;
  7. &lt;input ref={ref4} name=&quot;4&quot; onChange={otpBoxChange} type=&quot;number&quot; disabled /&gt;
  8. &lt;/div&gt;
  9. &lt;button disabled={false} type=&quot;submit&quot; className=&quot;btn btn-primary btn-submit text-dark&quot;&gt;{&quot;Submit&quot;}&lt;/button&gt;
  10. &lt;/form&gt;

This is a form for register. The items are centered with a definite width. The height is fixed and I want it to animate. However, for some reason, its not animation with the transition property on css.
How do I fix this now? I'm unable to figure it out.

答案1

得分: 1

CSS 无法直接动画化以 auto 设置的高度属性(https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)。
你可以尝试通过 JavaScript 使用 scrollHeight 属性 设置高度,或尝试已经在 这里 讨论过的众多解决方案。

英文:

CSS can't directly animate the height property when it is set with auto (https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties).
One thing you could try is setting the height via JavaScript using the scrollHeight property, or try one of the numerous solutions already discussed here.

huangapple
  • 本文由 发表于 2023年5月21日 23:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定