I am attempting to make a gallery, and in that gallery I want to have it so that when I mouseover a thumbnail I want to have a bigger version of that image pop up at the cursor, and then for it to disappear when you remove the cursor from the thumbnail.
Is there anyway to do this without hardcoding two sets of the image in the HTML code and just use the available images to, for example, onmouseover
create an element that shows the larger version of the image and using the img
src
of the hovered image that will then go away when removing the cursor from the element?
If I tried to explain it with code I guess it would look something like:
const image = document.getElementsByClassName('image');
for (let i = 0; i < image.length; i++) {
const picture = image[i];
picture.onmouseover = () => {
const img = document.createElement('img');
img.src = picture.src; //using the source of the available image to display it in a larger version
}
}
...and then removing the element when onmouseout
.
I'm sure there are some libraries that might make this easier, like jQuery, but I am trying to make it in JavaScript to understand it better.