I have three images-one shown, the other two with display:none
.
I’d like, when I press the button, for the script to check which image is currently being displayed and switch to the next image accordingly.
The buttons seem to call to the .js file properly, because the browser console returns the final else log.
function rightImage() {
var image1 = document.getElementById("stc-parking-map");
var image2 = document.getElementById("stc-first-floor");
var image3 = document.getElementById("stc-second-floor");
var image1Indicator = document.getElementById("directions-image1-indicator");
var image2Indicator = document.getElementById("directions-image2-indicator");
var image3Indicator = document.getElementById("directions-image3-indicator");
if (image1.style.display === "flex") {
image1.style.display = "none";
image2.style.display = "flex";
image1Indicator.style.backgroundColor = "rgba(0,0,0,0.92)";
image2Indicator.style.backgroundColor = "rgba(37,53,81,1.00)";
} else if (image2.style.display === "flex") {
image2.style.display = "none";
image3.style.display = "flex";
image2Indicator.style.backgroundColor = "rgba(0,0,0,0.92)";
image3Indicator.style.backgroundColor = "rgba(37,53,81,1.00)";
} else if (image3.style.display === "flex") {
image3.style.display = "none";
image1.style.display = "flex";
image3Indicator.style.backgroundColor = "rgba(0,0,0,0.92)";
image1Indicator.style.backgroundColor = "rgba(37,53,81,1.00)";
} else {
console.log("Everything Failed?");
}
}
function leftImage() {
const image1 = document.getElementById("stc-parking-map");
const image2 = document.getElementById("stc-first-floor");
const image3 = document.getElementById("stc-second-floor");
const image1Indicator = document.getElementById("directions-image1-indicator");
const image2Indicator = document.getElementById("directions-image2-indicator");
const image3Indicator = document.getElementById("directions-image3-indicator");
if (image1.style.display === "block") {
image1.style.display = "none";
image3.style.display = "block";
image1Indicator.style.backgroundColor = "rgba(0,0,0,0.92)";
image3Indicator.style.backgroundColor = "rgba(37,53,81,1.00)";
} else if (image3.style.display === "block") {
image3.style.display = "none";
image2.style.display = "block";
image3Indicator.style.backgroundColor = "rgba(0,0,0,0.92)";
image2Indicator.style.backgroundColor = "rgba(37,53,81,1.00)";
} else if (image2.style.display === "block") {
image2.style.display = "none";
image1.style.display = "block";
image2Indicator.style.backgroundColor = "rgba(0,0,0,0.92)";
image1Indicator.style.backgroundColor = "rgba(37,53,81,1.00)";
}
}
/*
CSS (There are some properties that belong to a class, so you'll see things like display:flex missing from containers)
*/
#visit-us-parking-and-directions-content {
justify-content: flex-end;
}
.peirce-directions-image {
width: 100%;
}
#stc-parking-map {
display: flex;
}
#stc-first-floor {
display: none;
}
#stc-second-floor {
display: none;
}
#directions-image-change-button-container,
#directions-image-number-indicator-container {
display: flex;
flex-direction: row;
justify-content: center;
width: 100%;
}
.directions-image-number-indicator {
height: 15px;
width: 33.33%;
border-right: solid;
border-color: rgba(224, 224, 219, 1.00);
background-color: rgba(0, 0, 0, 0.92);
}
.directions-image-number-indicator:first-child {
border-left: solid;
border-color: rgba(224, 224, 219, 1.00);
}
#directions-image1-indicator {
background-color: rgba(37, 53, 81, 1.00);
}
.directions-image-change-button {
padding: 38px 0 30px 0;
width: 50%;
height: 100%;
}
<div id="visit-us-parking-and-directions-container" class="section-container">
<div id="visit-us-parking-and-directions-content" class="section-content">
<h2>Parking and Directions</h2>
<div id="parking-directions-images-container">
<img id="stc-parking-map" class="peirce-directions-image" src="images/stc-parking-map.png" alt="parking-map">
<img id="stc-first-floor" class="peirce-directions-image" src="images/stc-first-floor.png" alt="first-floor">
<img id="stc-second-floor" class="peirce-directions-image" src="images/stc-second-floor.png" alt="second-floor">
<div id="directions-image-number-indicator-container">
<div id="directions-image1-indicator" class="directions-image-number-indicator"></div>
<div id="directions-image2-indicator" class="directions-image-number-indicator"></div>
<div id="directions-image3-indicator" class="directions-image-number-indicator"></div>
</div>
<div id="directions-image-change-button-container">
<button id="directions-left-image-button" class="directions-image-change-button" onClick="leftImage()">←</button>
<button id="directions-right-image-button" class="directions-image-change-button" onClick="rightImage()">→</button>
</div>
</div>
</div>
</div>
I’ve tried block and flex for the image display values. I put logs inside of the ifs at one point to see if it passed the if but failed to change the style-it did not pass the ifs, ever. I’ve tried both const
and var
for getting the elements.
2
Answers
Here is a good way to solve this problem, this solution can still be improved but is good none the less.
I am basically making an array of the image and the indicator elements. With every button click I iterate over them and check which element has the active class. I then replace the active with the appropriate inactive class. The next step is straightforward since we want the next element to be active (previous in case of left button) so we do
i+1
ori-1
. But we also don’t want to go out of bounds. So when we’re at either upper or lower limit I manually reset the value ofi
to the appropriate value for the next part of the code to run.Rather than hide all but one image and have to keep track of which ones to hide/show, the thing to do here is just have only one
img
element that is always visible and just dynamically change which image is thesrc
.To do this you put your image paths into an array. That way you can just use the array indices to iterate from one to the next.
You won’t have to deal with styles or involved if/then statements.
Here’s a simplified example: