So, I’m in a solo Hackathon. And I need to display images at the full viewport size which, which clicked, become the next picture. There’s four pictures in total and then it turns into the game. I made it like that because making four cut scenes in HTML and CSS would have ended me. So, is this possible?
I made divs in HTML before the game then I added the images into CSS. I’ve been messing around with JavaScript, I don’t get how to make it so though one image shows, then when it’s clicked, it disappears and is replaced by the second and so on and then the game starts.
Get an array of the image elements (like images = Array.from(imagecontainer.querySelectorAll("img"))
Add an active class to the first image (like images[0].class list.add("active"))
Store the currently active index in a variable (like currentImageIndex = 0)
Add an event handler listening for click events (like imagecontainer.addEventListener("click", (event) => { /* logic here */)
On click, if you aren’t on the final image, remove the active from the currently active index and add it to the image at the currently active index + 1 (like images[currentImageIndex].classList.remove(active), then images[currentImageIndex + 1].class list.add("active"))
Update the currently active index variable.
If the currently active index is the same as the last index (ie you’re on the last image and click once more), then do whatever it is you want to do next
This is rough. But it’ll work perfectly well. I’d probably use a class here to encapsulate the logic, or go slightly further & make it a web component
It seems easier to put the images in an array and index into the array.
Index into the array, if you get back an image set the source and increment the index, if you do not get back an image you have reached the end of the array and can start the game.
array of images
image element
index
click handler
get image from array using index
if image
set image
increment index
else
start game