I have this code:
HTML
<div id='features-buttons'>
<button class='features-button' id='theButton' onclick='featureImage1()' type='button'><i class='fa fa-briefcase'/> Home Office</button>
<button class='features-button' id='theButton' onclick='featureImage2()' type='button'><i class='fa fa-plus-circle'/> The Mezzaine (Add On)</button>
</div>
<img class='features-image' id='theImage' src='https://media.designcafe.com/wp-content/uploads/2019/11/17054904/interior-designer-or-decorator-make-your-choice-1.jpg' style='border-radius:30px;'/>
CSS
.features-image{
width:100%;
height:600px;
object-fit: cover;
}
.features-button{
width: 200px;
padding: 10px 30px;
font-family: 'Jost';
font-size: 20px;
border: 0px;
border-radius: 10px;
cursor: pointer;
background: #fff;
color: #453011;
margin: 5px 25%;
}
JS
function featureImage1(){
document.getElementById("theImage").src="https://media.designcafe.com/wp-content/uploads/2019/11/17054904/interior-designer-or-decorator-make-your-choice-1.jpg";
}
function featureImage2(){
document.getElementById("theImage").src="https://hommes.studio/wp-content/uploads/Get-To-Know-Interior-Design-Trends-2022_16.jpg";
}
I have image changer code this is by replacing the source of the image, but I want to add transition when the image is changing.
You can use
CSStransitions. Here's your code updated, I added a transition property to the.features-imageclass with a duration of0.5 secondsand an easing function ofease-in-out. I also added two new classes:.fadeand.show, which will control theopacityof the image during the transition.In
JavaScriptcode, I added the.fadeclass to the image before changing itssrcattribute. After a 500ms delay (the same duration as the transition), I change thesrcattribute and remove the.fadeclass. I then add the.showclass to make the image fully visible again. This causes the fade transition to occur between the two images: