1

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

Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42
AlbertMunichMar
  • 1,680
  • 5
  • 25
  • 49

3 Answers3

0

Use opacity set opacity:0 to hide element.

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

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>
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47
0

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.

Aravind S
  • 2,347
  • 2
  • 12
  • 21
0

That is because you are using 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.

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:

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>
Aayush Sharma
  • 779
  • 4
  • 20
  • 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 02 '18 at 07:37
  • 3) I need to use opacity !important, if I want to make it work. Why? – AlbertMunichMar Jul 02 '18 at 07:38