Pre-Loading Images

Preloading images with JavaScript

Yes. This should work on all major browsers.

Preloading then adding additional images after page load to an image gallery on button click

Generally you would create an image with JavaScript through either document.createElement('img') or the Image() constructor. Both result an in instance of an HTMLImageElement.

With this, you'll have an image that is not connected to the DOM, so it's not visible to you or the user. You can use this element to load the image behind the scenes by setting the image' src property.

Then by listening to the onload event you can determine whenever the image has finished loading. From here you could continue your flow by adding the image to the DOM and, for example, fade it in with animation.

The example below shows this process in the form of a function that returns a Promise. This promise will resolve whenever the load event has been triggered.

const preloadImage = src => 
new Promise(resolve => {
const image = new Image();
const onLoad = () => {
resolve(image);
};
image.addEventListener('load', onLoad, {once: true});
image.src = src;
});

Using it should be like this:

const src = 'http://example.com/my-image.jpg';
preloadImage(src).then(image => {
// Here the image has been loaded and is available to be appended, or otherwise.
document.body.append(image);
});

In your case you would loop over each image, call the function while passing the URL of the image, and append the image to the DOM when it's finished loading.

You can handle any animations, like fade-ins with CSS.

Real world implementation

So how should you implement this in your project? You'll need to start at the point where you create your images. Currently your images are created as strings. But strings are just strings, they aren't HTML elements, yet.

I'd recommend that you'll create a placeholder for each image. This placeholder could visually indicate that an image is loading and act as a wrapper for the image. Add this placeholder immediately to the pItems element.

Then load the image for each Image in your data.projects array by calling the preloadImage. Whenever the image is loaded, append it to the placeholder we've just created. You should now have the effect that first a placeholder is added and the images are starting to appear one by one.

The same logic should be applied for the load more loop.

...
}).then(function (data){
for (let i = 0; i < itemsStart; i++) {

// Create a <div class="placeholder"> which should act as a placeholder / wrapper.
const placeholder = document.createElement('div');
placeholder.classList.add('placeholder');

// Create the image based on the Image value.
// Whenever the image is loaded, add it to the placeholder.
const src = data.projects[i].Image;
preloadImage(src).then(image => {
placeholder.append(image);
});

// Immediately add the placeholder.
// This line doesn't wait for preloadImage to finish because preloadImage is asynchronous. Look into Promises if that is new to you.
pItems.append(placeholder);
}

...
});

From here you've got control over how the placeholder should look and any animations an image inside that placeholder should have.

Preloading CSS Images

I can confirm that my original code seems to work. I was casually sticking to an image with a wrong path.

Here's a test : http://paragraphe.org/slidetoggletest/test.html

<script>
var pic = new Image();
var pic2 = new Image();
var pic3 = new Image();
pic.src="images/inputs/input1.png";
pic2.src="images/inputs/input2.png";
pic3.src="images/inputs/input3.png";
</script>

image preloading javascript

Doesn't seem like your function will preload anything. Apart from the random image selected which loads normally, the other images in the array will not be called at all.

You might want to check out JavaScript Preloading Images and Function to preload images? instead.



Related Topics



Leave a reply



Submit