Color transition not working with setTimeout. Element should appear smoothy [duplicate]


Color transition not working with setTimeout. Element should appear smoothy [duplicate]



This question already has an answer here:



Color transition works, if I don't hide the element and then make it appear.




const name = document.querySelector(".name");

setTimeout(() => {
name.classList.remove("hide");
name.classList.add("color-blue")
// name.style.color = "blue";
}, 2000)


.name {
background-color: orange;
display: inline-block;
padding: 5px;
transition: color 10s;
color: transparent;
}
.color-blue { color: blue; }

.hide { display: none }


<div class="name hide">
Lebron James
</div>



If I don't use the class hide and I don't remove it later, the transition works. By hiding it, it does not work.



No clue what's going on



Thanks for your help



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





change hide to opacity:0 and try
– Aravind S
Jul 2 at 7:01


hide


opacity:0




3 Answers
3



here is working code




const name = document.querySelector(".name");

setTimeout(() => {
name.classList.remove("hide");
name.classList.add("color-blue")
// name.style.color = "blue";
}, 2000)


.name {
background-color: orange;
display: inline-block;
padding: 5px;
transition: color 10s;
color: transparent;
}
.color-blue { color: blue; }

.hide { opacity: 0 }


<div class="name hide">
Lebron James
</div>



http://jsfiddle.net/Aravi/39pdLarn/2/



the display has limitations in animated BUT you can use visibility and opacity for transitions.





hey... the link does not work
– לבני מלכה
Jul 2 at 7:09





updated...thanks!
– Aravind S
Jul 2 at 7:10



Use opacity set opacity:0 to hide element.


opacity


opacity:0



Learn about opacity:https://www.w3schools.com/css/css_image_transparency.asp


opacity




const name = document.querySelector(".name");

setTimeout(() => {
name.classList.remove("hide");
name.classList.add("color-blue")
// name.style.color = "blue";
}, 2000)


.name {
background-color: orange;
display: inline-block;
padding: 5px;
transition: color 10s;
color: transparent;
}
.color-blue { color: blue; }

.hide { opacity:0; }


<div class="name hide">
Lebron James
</div>





that what you mean??
– לבני מלכה
Jul 2 at 7:07





I don't get why my strategy was wrong. Why it does not work from display none to display inline-block
– AlbertMunichMar
Jul 2 at 7:38



That is because you are using display: none;.


display: none;



CSS cannot show animation if you set display to none. You can make use of opacity: 0 to hide the element but remember to add that to transition as well otherwise it will not show any color transition.


display


none


opacity: 0


transition




const name = document.querySelector(".name");

setTimeout(() => {
name.classList.add("hide");
name.classList.add("color-blue")
// name.style.color = "blue";
}, 2000);


.name {
background-color: orange;
display: inline-block;
padding: 5px;
transition: color 10s, opacity 10s;
color: transparent;
}

.color-blue {
color: blue;
}

.hide {
opacity: 0;
}


<div class="name">
Lebron James
</div>



Alternatively, if you want to use display: none (i.e, remove the element from DOM) then you can update your JS code as follow:


display: none




const name = document.querySelector(".name");

setTimeout(() => {
setTimeout(() => {
name.classList.add("hide");
}, 10000);
name.classList.add("color-blue")
// name.style.color = "blue";
}, 2000)


.name {
background-color: orange;
display: inline-block;
padding: 5px;
transition: color 10s;
color: transparent;
}

.color-blue {
color: blue;
}

.hide {
display: none
}


<div class="name">
Lebron James
</div>





1) Surprisingly, with opacity: 0 from the begining, the element does not occupy space in the DOM so I don't need the extra setTimeout 2) I find weird that I get rid of the class hide with display none, because then the display has the value inline-block. I don't get why the displays do not work with animations
– AlbertMunichMar
Jul 2 at 7:37






3) I need to use opacity !important, if I want to make it work. Why?
– AlbertMunichMar
Jul 2 at 7:38

Popular posts from this blog

How to add background colour in existing image using Swift?

What's the difference between DDS and SOME/IP?

How to make file upload 'Required' in Contact Form 7?