I am currently working on a project where I have a list of images that need to be hidden or shown based on the click event of specific <li>
elements. While I have managed to achieve this functionality successfully, I am facing an issue with white space appearing above or below the image that is displayed. Below is the code snippet I am using:
HTML
<div class="img-container">
<img id="img1" src="img/image1.jpg" />
<img id="img2" src="img/image2.jpg" />
<img id="img3" src="img/image3.jpg" />
</div>
CSS
.img-container{
text-align: center;
visibility: hidden;
margin-top: 20px;
}
JS
var img1 = document.getElementById('img1');
var img2 = document.getElementById('img2');
var img3 = document.getElementById('img3');
("li:nth-child(2)").on('click', function() {
img1.style.visibility = 'visible';
img2.style.visibility = 'hidden';
img3.style.visibility = 'hidden';
});
("li:nth-child(3)").on('click', function(){
img2.style.visibility = 'visible';
img1.style.visibility = 'hidden';
img3.style.visibility = 'hidden';
});
("li:last-child").on('click', function() {
img3.style.visibility = 'visible';
img2.style.visibility = 'hidden';
img1.style.visibility = 'hidden';
});
Despite experimenting with the display: hidden
CSS property along with .toggle()
, I haven't been able to resolve the issue of white space around the hidden image. As I am new to JS/jQuery, I would appreciate any guidance on how to tackle this problem effectively. Thank you.