1/ initial load doesn’t display the loading.gif before displaying first image as page loads.
- ok. then we will display the loading.gif image and hide the first image on load
that can be done with these codes;
$().loadImage($("#largeImg"));
<img src="loading.gif" id="loader_image" />
<img src="http://www.rnel.net/images/tutorials/for_frame-16394.jpg" style="display:none;" alt="test1" id="largeImg" />
2/ the image space collapses to the size of the loading.gif, is there a way to keep the same image size as the largeImg, but have the loading.gif centered inisde?
- yes. play with your css style until you achieved what you want - class=“thumbs” and/or other codes you have
Actually in relation to (3), i realised that in fact it’s not displaying the loading.gif until the new image loads, but rather using a timer? How can this be changed to firing on image load?
- .load(). will fire these images onload - however I consider my opinion that your images are relatively small in file size hence loading.gif image will almost not show up anymore. It seems that you would like to show the loading.gif image every time the image loads up so I did it with setTimeout()
here’s how to fire them up on .load();
$("#largeImg").attr({ src: largePath, alt: largeAlt }).show();
$("#largeImg").load(function(){
$("#loader_image").hide();
});
on the other hand, this is how to commence them on setTimeout
setTimeout(function(){
$("#loader_image").hide();
$("#largeImg").show();
},1000
);
here is the full code - decide what fits in your requirements
$(document).ready(function(){
$().loadImage($("#largeImg"));
$(".thumbs a").click(function(){
$().loadImage($(this));
return false;
});
});
jQuery.fn.extend({
loadImage:function(myObject){
var largePath = myObject.attr("href");
var largeAlt = myObject.attr("title");
$("#loader_image").show();
$("#largeImg").hide().attr({ src: largePath, alt: largeAlt });
$(".thumbs a").removeClass("viewing");
myObject.addClass("viewing");
setTimeout(function(){
$("#loader_image").hide();
$("#largeImg").show();
},1000
);
/*
$("#largeImg").attr({ src: largePath, alt: largeAlt }).show();
$("#largeImg").load(function(){
$("#loader_image").hide();
});
*/
return false;
}
});
<div class="thumbs">
<a href="http://www.rnel.net/images/tutorials/for_frame-16394.jpg" title="frame1" class="viewing">
1
</a>
<a href="http://www.rnel.net/images/tutorials/for_broken_egg-16397.jpg" title="egg">
2
</a>
<br />
<img src="loading.gif" id="loader_image" />
<img src="http://www.rnel.net/images/tutorials/for_frame-16394.jpg" style="display:none;" alt="test1" id="largeImg" />
</div>