Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

javascript - Why are transitions not working for elements created in by react

I have this css applied to elements in react

.my-float{

    display: flex;
    justify-content: center;
    align-content: center;
    font-size: 1em;
    transition: 2s;
    transition-timing-function: ease-in;
}

and in my component I have

 {(!visibility)&&<i className="fa fa-angle-up my-float" ></i>}
 {visibility&&<i className="fa fa-times my-float" ></i>}

As you may see I have the ability to toggle these two elements, the challenge is the transitions are not applied. How can I make sure the transitions work? I want the elements to fade in and fade out when appearing and disappearing respectively.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With your code, you are adding/removing your <i> from DOM depending on visibility

So you won't have any CSS-transition if your icon is removed from DOM, or if your icon has just been added to DOM.

Your icon needs to be on DOM everytime so CSS transitions can apply

<>
  <i className={`fa fa-angle-up my-float ${visibility && 'hidden'} />
  <i className={`fa fa-times my-float ${!visibility && 'hidden'} />
</>

Or using https://www.npmjs.com/package/clsx

import cn from 'clsx';

<>
  <i className={cn('fa fa-angle-up my-float', visibility && 'hidden')} />
  <i className={cn('fa fa-times my-float', !visibility && 'hidden'} />
</>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...