Wait For Image To Be Loaded Before Displayed
Start with visibility:hidden
and change it to visibility:visible
on the image's onload event handler.
edit: example
In your HTML, have:
<img onload="imgLoad(this)" style="visibility:hidden" ... />
In your script, have:
function imgLoad(img) {
img.style.visibility='visible';
}
Wait for image to be fully loaded before displayed on page?
What comes to mind without actually used or tested it is wrapping the image. I'll use jQuery for the sample
<div class="loading"><img/></div>
CSS could be
.loading { display: inline-block; }
.ie6 .loading, .ie7 .loading { display: inline; } /* these could be set by http://www.modernizr.com/ */
.loading img {
visibility: hidden;
background: url('path/to/loading.gif') center center no-repeat transparent;
vertical-align: bottom; /* or display: block; to kill the inline white space */
}
JavaScript (jQuery)
$(function(){
$('.loading img').load(function(){
$(this).css('visibility','visible');
});
});
Display page when all background images are loaded
You could show an overlay with loading animation
<div id="preloader"></div>
#preloader
{
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: 9999;
background: #FFFFFF url('URL TO AN ANIMATED LOADING GIF') no-repeat center center;
}
and set it to display: none
after all content is loaded
window.onload = function() {
document.querySelector('#preloader').style.display = "none";
};
JQuery bind event when CSS background-image is fully loaded
You could use my jQuery plugin called waitForImages that would help with this...
$("#box").css("background-image", "url(image.php)").waitForImages({
waitForAll: true,
finished: function() {
// Background image has loaded.
}
});
Otherwise, to do it manually...
var image = 'image.php',
img = $('<img />');
img.bind('load', function() {
// Background image has loaded.
});
img.attr('src', image);
$('#box').css('background-image', 'url(' + image + ')');
Related Topics
How to Raw Url Encode/Decode in JavaScript and Ruby to Get the Same Values in Both
Sorting HTML Table with JavaScript
How to Convert a Float Number to a Whole Number in JavaScript
Best Way to Get All Selected Checkboxes Values in Jquery
Chrome 65 Blocks Cross-Origin <A Download>. Client-Side Workaround to Force Download
What Is the Current State of the Art in HTML Canvas JavaScript Libraries and Frameworks
Why Is Array.Push Sometimes Faster Than Array[N] = Value
Is It True That Every Function in JavaScript Is a Closure
Jquery Parallax Scrolling Effect - Multi Directional
Difference Between Statement and Function
How to Get First and Last Day of the Current Week in JavaScript
How to Initialize an Array's Length in JavaScript
Sending Message from a Background Script to a Content Script, Then to a Injected Script
Why Do We Need to Use Radix Parameter When Calling Parseint
How to Remove an Item from an Array in Angularjs Scope
How to Capitalize the First Letter of Each Word in a String Using JavaScript