I searched for a way to change all the images in a row when hovering, but I could not find any; I used the following script in which I want to loop through the images and change them. Is it possible in this way? Is there a better way?
let imageSources = ["images/cat1.jpg", "images/cat2.jpg", "images/cat3.jpg"];
let images = document.querySelectorAll('.img-fluid.img-thumbnail');
function changeImg(index) {
return function() {
images[index - 1].src = imageSources[index];
};
}
function restoreImg(index) {
return function() {
images[index - 1].src = "images/frozen.jpg";
};
}
images.forEach(function(index) {
image[index].addEventListener('mouseover', changeImage(index));
image[index].addEventListener('mouseout', restoreImage(index));
};
<div class="row">
<div class="col-md-4">
<img src="images/frozen.jpg" class="img-fluid img-thumbnail mb-4" alt="">
</div>
<div class="col-md-4">
<img src="images/frozen.jpg" class="img-fluid img-thumbnail mb-4" alt="">
</div>
<div class="col-md-4">
<img src="images/frozen.jpg" class="img-fluid img-thumbnail mb-4" alt="">
</div>
</div>
I tried in multiple ways, but the images do not change when I hover over them.
2
Answers
You could add the "hover images" to the HTML itself and then show/hide the images using hover in CSS. Advantages are that all images are loaded from the start and you don’t need to change the DOM.
Now, I don’t know if the images are important to the semantics of the HTML. If not and the images are just used as icons you could move alle the images to the CSS:
hi dear i fix your code base your requirement.