My transition effect is working smoothly, but I am having trouble setting it up for the image. How can I resolve this issue?
I attempted the following:
var lightsOff = document.querySelector("#lights-off");
var lightsOn = document.querySelector("#lights-on");
lightsOff.style.display = "block";
lightsOn.style.display = "none";
And also this:
var lightsOff = document.querySelector("#lights-off");
var lightsOn = document.querySelector("#lights-on");
lightsOff.style.display = "none";
lightsOn.style.display = "block";
Below is the complete code:
function myFunction() {
var elements = document.querySelectorAll(".one, .three, .five");
elements.forEach(function(element) {
element.classList.toggle("dark-mode");
if (element.classList.contains("dark-mode")) {
if (element.classList.contains("one")) {
var logoElement = element.querySelector('.logo');
var h2Element = element.querySelector('h2');
var sloganElement = element.querySelector('.slogan');
logoElement.style.setProperty('color', 'white');
sloganElement.style.setProperty('color', 'white');
h2Element.style.setProperty('color', 'white');
} else if (element.classList.contains("three")) {
var textThreeOne = element.querySelector('.text-threeone');
var textThreeTwo = element.querySelector('.text-threetwo');
var p = element.querySelector('.text-threetwo + p');
textThreeOne.style.setProperty('color', 'white');
textThreeTwo.style.setProperty('color', 'white');
p.style.setProperty('color', 'white');
var lightsOff = document.querySelector("#lights-off");
var lightsOn = document.querySelector("#lights-on");
lightsOff.style.display = "block";
lightsOn.style.display = "none";
var blackmoon = document.querySelector(".moon");
blackmoon.style.display = "block";
} else if (element.classList.contains("five")) {
var inputs = element.querySelectorAll('input');
var textarea = element.querySelector('textarea');
var submitBtn = element.querySelector('input[type="submit"]');
inputs.forEach(input => {
input.style.setProperty('background-color', '#1f1f1f');
input.style.setProperty('color', '#ffffff');
});
textarea.style.setProperty('background-color', '#1f1f1f');
textarea.style.setProperty('color', '#ffffff');
submitBtn.style.setProperty('background-color', '#1f1f1f');
// Add mouseover color
submitBtn.style.setProperty('transition', 'background-color 0.2s');
submitBtn.addEventListener("mouseover", function() {
submitBtn.style.setProperty('background-color', '#101010');
});
submitBtn.addEventListener("mouseout", function() {
submitBtn.style.setProperty('background-color', '#1f1f1f');
});
var contact = element.querySelector('#contact');
var pOne = element.querySelector('.five>p+p');
var pTwo = element.querySelector('.five>p+p+p');
contact.style.setProperty('color', 'white');
pOne.style.setProperty('color', 'white');
pTwo.style.setProperty('color', 'white');
} else {
element.style.setProperty('color', 'white');
}
} else {
if (element.classList.contains("one")) {
var logoElement = element.querySelector('.logo');
logoElement.style.setProperty('color', '#353535');
var sloganElement = element.querySelector('.slogan');
sloganElement.style.setProperty('color', '#353535');
var sloganElement = element.querySelector('h2');
sloganElement.style.setProperty('color', '#353535');
var blackmoon = document.querySelector(".moon");
blackmoon.style.display = "none";
} else if (element.classList.contains("three")) {
var textThreeOne = element.querySelector('.text-threeone');
var textThreeTwo = element.querySelector('.text-threetwo');
var p = element.querySelector('.text-threetwo + p');
var lightsOff = document.querySelector("#lights-off");
var lightsOn = document.querySelector("#lights-on");
lightsOff.style.display = "none";
lightsOn.style.display = "block";
p.style.setProperty('color', '#353535');
textThreeOne.style.setProperty('color', '#353535');
textThreeTwo.style.setProperty('color', '#353535');
} else if (element.classList.contains("five")) {
var inputs = element.querySelectorAll('input');
var textarea = element.querySelector('textarea');
var submitBtn = element.querySelector('input[type="submit"]');
inputs.forEach(input => {
input.style.setProperty('background-color', '#efefef');
input.style.setProperty('color', 'black');
});
textarea.style.setProperty('background-color', '#efefef');
textarea.style.setProperty('color', 'black');
submitBtn.style.setProperty('background-color', '#437bff');
submitBtn.style.setProperty('color', 'white');
// Add mouseover color
submitBtn.style.setProperty('transition', 'background-color 0.2s');
submitBtn.addEventListener("mouseover", function() {
submitBtn.style.setProperty('background-color', '#133edb');
});
submitBtn.addEventListener("mouseout", function() {
submitBtn.style.setProperty('background-color', '#437bff');
});
var contact = element.querySelector('#contact');
var pOne = element.querySelector('.five>p+p');
var pTwo = element.querySelector('.five>p+p+p');
contact.style.setProperty('color', '#353535');
pOne.style.setProperty('color', '#353535');
pTwo.style.setProperty('color', '#353535');
}
}
});
}
.one,
.three,
.five {
transition: 1500ms;
}
<div>
<button class="darkmodebtn" onclick="myFunction()"></button>
<img id="lights-off" src="../images/darkmode.png" style="display: none;">
<img id="lights-on" src="../images/ABC.png">
</div>