My goal is to display two images using a single <img />
tag. The first small image will be loaded and shown using the src
attribute, while the second large image will be contained within the data-src
attribute. Only one image will be displayed at a time, with the small image initially loading and then being replaced by the large image once it has fully loaded in the background. I have code that switches the large image from the data-src
attribute to the src
attribute.
$(document).ready(function(){
$("#image4").load(function(){
var imgDefer = document.getElementsByTagName('img');
for (var i=0; i<imgDefer.length; i++) {
if(imgDefer[i].getAttribute('data-src')) {
imgDefer[i].setAttribute('src',imgDefer[i].getAttribute('data-src'));
}
}
});
});
This approach allows me to show the small image first without having to wait for the large image to load completely. However, I am currently experiencing an issue where both small and large images are loading concurrently when the page loads. It's important to note that these images feature drag and zoom functionality. You can view the live version of this code here: