There's 2 things that you would want to do when you set the new image.
- reset the opacity to 0
- change the actual image and transition opacity back to 1.
you're setting the image just fine.
You're not resetting the opacity.
You can achieve this by
- removing the class
- triggering a reflow, and render the element with opacity:0
- applying a new class
I've added logic to your changeit
function to remove the class and trigger the reflow.
I've also moved the 'transition' css rule to the other classes. so that when we remove a class, it will immediately snap to 0 opacity
function changit(color) {
// remove the old classs
document.getElementById('cont').classList = ""
// trigger a 'reflow' so the dom is updated
document.getElementById('cont').offsetWidth;
// apply the new class.
document.getElementById('cont').classList = color;
}
#cont {
position: relative;
width: 100px;
height: 100px;
display: block;
}
#cont::before {
opacity: 0;
width: 93%;
height: 100%;
position: sticky;
content: "";
display: block;
}
.red::before {
opacity: 1!important;
transition: opacity 2s ease;
background: url(https://cdn.sstatic.net/Img/teams/teams-illo-free-sidebar-promo.svg) no-repeat;
}
.blue::before {
opacity: 1!important;
transition: opacity 2s ease;
background: url(https://www.gravatar.com/avatar/24780fb6df85a943c7aea0402c843737?s=64&d=identicon&r=PG) no-repeat;
}
.black::before {
opacity: 1!important;
transition: opacity 2s ease;
background: black;
}
<div id="cont">
</div>
<button onclick="changit('red');">make red</button>
<button onclick="changit('blue');">make blue</button>
<button onclick="changit('black');">make black</button>
using an animation:
if your animation has a single name, it will not automatically reset the animation when changing a class.
you can work around this by defining separate animation names in css.
function changit(color) {
document.getElementById('cont').classList = color;
}
#cont {
position: relative;
width: 100px;
height: 100px;
display: block;
}
#cont::before {
width: 93%;
height: 100%;
position: sticky;
content: "";
display: block;
}
.red::before {
animation: appear-red 1s;
background: url(https://cdn.sstatic.net/Img/teams/teams-illo-free-sidebar-promo.svg) no-repeat;
}
.blue::before {
animation: appear-blue 1s;
background: url(https://www.gravatar.com/avatar/24780fb6df85a943c7aea0402c843737?s=64&d=identicon&r=PG) no-repeat;
}
.black::before {
animation: appear-black 1s;
background: black;
}
@keyframes appear-red {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes appear-blue {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes appear-black {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<div id="cont">
</div>
<button onclick="changit('red');">make red</button>
<button onclick="changit('blue');">make blue</button>
<button onclick="changit('black');">make black</button>