Why the content loaded, but the images still loading?
How to load a content (include content’s images) and then fadein?
js
$(document).ready(function(){
$('#contents').fadeOut('slow', function(){
$(this).load("url", {}, function(){
$("#contents").fadeIn('slow', function(){
alert('Faded in!');
});
});
});
});
This is not so straightforward. You need to get load handlers onto the dynamically loaded images, but without modifying the source HTML you can’t do that until they’ve already started loading which makes it a bit more complicated because some might actually complete before you check. So, you could do something like this which gets all the image objects in the dynamically loaded content and then checks each one to see if it has finished loading. If it has not finished loading, then an onload handler is installed so we can count when the last one finished loading. When all are done loading, we can then do the
fadeIn():Or, for a little more general purpose way of solving the problem, here’s a generic jQuery method that loads content and then calls the callback when both the content and all images in the content have finished loading. This would be a lot more reusable and probably make your code more readable. You use it just like the
.load(url, data, fn) method:So, with this new method, your code would just be this: