I recently got into this problem, I have a ‘div’ element that is set to have:
JavaScript
x
9
1
#add-wallet-div {
2
width: 10px;
3
height: 10px;
4
background: blue;
5
border-radius: 10px;
6
opacity: 0;
7
transition: 1s;
8
}
9
and when I add to this element the ‘toggled’ class it expand:
JavaScript
1
6
1
#add-wallet-div.toggled {
2
height: 300px;
3
width: 200px;
4
opacity: 1;
5
}
6
The problem here is that I want the opacity to go from a value of 0 to a value of 1 instantaneously, but since I have to set transition time to 1s this isn’t happening.
I saw a couple of solution to this problem but all of them use jQuery, which I’d prefer not to use.
I also tried this:
JavaScript
1
18
18
1
#add-wallet-div {
2
width: 10px;
3
height: 10px;
4
background: blue;
5
border-radius: 10px;
6
opacity: 0;
7
}
8
9
#add-wallet-div.toggled {
10
height: 300px;
11
width: 200px;
12
transition: 1s;
13
}
14
15
#add-wallet-div.toggled2 {
16
opacity: 1;
17
}
18
But nothing changed.
Advertisement
Answer
I just found a way to solve this:
JavaScript
1
16
16
1
#add-wallet-div {
2
width: 10px;
3
height: 10px;
4
background: blue;
5
border-radius: 10px;
6
opacity: 0;
7
transition-duration: 1s;
8
transition-property: width, height;
9
}
10
11
#add-wallet-div.toggled {
12
height: 300px;
13
width: 200px;
14
opacity: 1;
15
}
16
Basically using the ‘transition-property’ property I tell on wich proprties the transition should have an effect.
Here’s a more in-depth explanation.