So, I’m trying to develop a sort of visual novel, and I’m trying to make a code to replace other images when a specific image appears, (for example, if "happy" is onscreen i want "sad" to be hidden so the pictures don’t overlap eachother)
Here’s my code:
<style>
section {
display: none;
}
.d-block {
display: block;
}
</style>
<section class="d-block">Horace</section>
<section>Ursa</section>
<section><img id=sad src=https://png.pngtree.com/png-clipart/20220716/ourmid/pngtree-banana-yellow-fruit-banana-skewers-png-image_5944324.png></section>
<section><img id=neutral src=https://www.shutterstock.com/image-photo/banana-bunch-five-600nw-1057197824.jpg></section>
<section><img id=happy src=https://t4.ftcdn.net/jpg/05/65/93/25/360_F_565932566_6bll77MbikvLen4pPoLVvlrqmvKTZ7Nw.jpg></section>
<section>Thus</section>
<button>Next</button>
<script>
document.addEventListener("DOMContentLoaded", () => {
const TextShower = document.querySelector('button');
TextShower.addEventListener('click', function() {
let hidden_section = document.querySelector('section:not(.d-block)');
if (hidden_section != null) {
hidden_section.classList.add('d-block');
} else {
TextShower.disabled = true;
}
});
const Happy = document.getElementById("happy")
if (happy.hasAttribute("d-block")) {
document.getElementById("sad","neutral").removeAttribute("d-block")
}
});
</script>
const Happy = document.getElementById("happy")
if (happy.hasAttribute("d-block")) {
document.getElementById("sad","neutral").removeAttribute("d-block")
}
});
this is the if statement i tried to create to replace the portraits. did not work
2
Answers
So if I get the question right you are trying to show one image at a time on button click [If "happy" shown hide the "sad/neutral" or vice versa.]
NOTE: You can run the code in the codePen for the quick verification and it work just fine.
Here’s how you can do it.
If you want to undertsand what happend here you go:
title
for the page in thehead
section.<style>
tag in the<head>
section, we define CSS rules for styling the image.display:none
for images inside thesection
hide the images by-default..active
that makes images visible asdisplay:block
.NOTE Happy image have the
.active
class for now.section
with thesad
andneutral
being hidden by default.Happy
image have the class.active
hence its displayed.DOMContentLoaded
,we execute code inside the callback function.section
and store them inside theimages
variable.currentIndex
to keep track of the currently active image.next button
add the event listener forclick
event..active
class from the currently active image ["happy"], hiding it.currentIndex
..active
class to the next image, making it visible.This way, clicking the button toggles between the images, showing only one image at a time. The images cycle through in a loop, so after reaching the last image, clicking the button will show the first image again.
NOTE — its better to have the
toggleClass
rather than adding or removing class separately or based on attribute add or remove class.Integrate the answer and have the logic work for your own code.
Keep Learning and keep exploring.
Happy Coding and have a good day❤️.