Controlling Image Load Order in HTML

Dynamically loading in order images from the server (for html gallery)

Here you have something implementin the code from that answer you posted here

<div class="ws_images"  >
<ul>
<?php // start looping the pics
$inc = 0;
foreach (new DirectoryIterator('gallery/data1/images/') as $file) {
if($file->isDot()) continue;
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file->getFilename()); //removing the extension
?>

<li>
<img class='imgs' img-name="<?php echo $file->getFilename(); ?>" src="" alt="<?php echo $withoutExt ?>" title="<?php echo $withoutExt; ?>" id="wows1_<?php echo $inc; $inc++; ?>"/>
</li>
<?php } // ending the loop ?>

</ul>
</div>



<script>
var imgAddresses = [];

$('.ws_images ul li').each(function(){
imgAddresses.push($(this).find('img').attr('img-name'));
});



function loadImage(counter) {
//Break out if no more images
if(counter==imgAddresses.length) { return; }

//Grab an image obj
var I = document.getElementById("wows1_"+counter);

//Monitor load or error events, moving on to next image in either case
I.onload = I.onerror = function() { loadImage(counter+1); }

//Change source (then wait for event)
I.src = 'gallery/data1/images/'+imgAddresses[counter];
}

loadImage(0);
</script>

You need to test this code because i didn't test it but i wrote it just to give you an idea how to do that

Preload image queue

You can try to use a hidden <img> tag at the top of your body to force the browser to load it earlier

Something like this:

<!DOCTYPE html>
<html>
<head>
<!-- ... -->
</head>
<body>
<img src="your/preloaded/image.png" style="display: none;"/>
<!-- ... -->
</body>
</html>

How to provide feedback while loading thumbnails and control the load order

Is this a favorable way to do this, or
are there better and faster possibilities?

One of the biggest speed improvements you will get is reducing the overall number of requests that you are making.

If you've got access to an image processing library on the server, it might be worth considering writing a script to render the all the thumbnails for each div into one large contact sheet image.

You could use an image map to figure out which full-res picture to display when the user clicks on the contact sheet (or you could possibly use a CSS sprite technique to render the correct thumbnail as a background on your link to the full-res picture, if you wanted to highlight the border of the thumbnail on mouseover).

HTH :^)

load specific image before anything else

As long as the "loading..." image is positioned before any other html elements, it should load first. This of course depends on the size of the image. You could put the loading div right after the tag and position it using 'position:absolute'.
Regarding the code to remove the loading screen, one method is to do the following.

  • Put all the images, scripts that need to be loaded in a hidden div (display: none)
  • Set up a variable that will hold the total of the images / scripts to be loaded
  • Set up a counter variable
  • Attach to each image / script the "onload" event
  • Everytime the "onload" event is triggered it will call a function that will increment the counter variable and check if the value of the counter equals the value of the total variable
  • If all resources have been loaded, fire a custom event that will show the div with the images, and hide the div with the loading screen.

The code below isn't testes so it might not work. Hope it helps

var totalImages = 0;
var loadCounter = 0;

function incrementLoadCounter() {
loadCounter++;
if(loadCounter === totalImages) {
$(document).trigger('everythingLoaded');
}
}

function hideLoadingScreen() {
$('#loadingScreen').hide();
$('#divWithImages').show();
}

$(document).ready(function(e) {
$('#loadingScreen').bind('everythingLoaded', function(e) {
hideLoadingScreen();
});
var imagesToLoad = $('img.toLoad');
totalImages = imagesToLoad.length;

$.each(imagesToLoad, function(i, item) {
$(item).load(function(e) {
incrementLoadCounter();
})
});
})

Controlling the load order of css files

The order they are specified in the HTML file is the only thing that should count for the final result.

Either way: load your jQuery UI CSS from elsewhere, I don't think code.jquery.com is supposed to be used as a CDN.

That should bring down loading times massively. 2 seconds is way too much for a measly style sheet.



Related Topics



Leave a reply



Submit