i am wanting to implement a solution where:
i have tried:
html:
<div id="content">
<!--this stuff takes a long time to load-->
<img src="http://lorempixel.com/1920/1920/">
</div>
<div id="loading">
<!-- this is the loading gif -->
<img src="http://lorempixel.com/400/200/">
</div>
js:
// when user browses to page
$('#content').hide();
$('#loading').show();
// then when div #content has loaded, hide div #loading and fade in div #content
$('#content').bind('load', function() {
$('#loading').hide();
$('#content').fadeIn('slow');
});
here is the jsfiddle i am working on:
http://jsfiddle.net/rwone/Y9CQ4/
thank you.
To toggle a div visibility in jQuery, use the toggle() method. It checks the div element for visibility i.e. the show() method if div is hidden. And hide() id the div element is visible. This eventually creates a toggle effect.
To hide an element, set the style display property to “none”. document. getElementById("element").
We hide the divs by adding a CSS class called hidden to the outer div called . text_container . This will trigger CSS to hide the inner div.
According to .load(), the event should fire, when
The load event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.
So, you cannot bind the load event handler to a div
tag. When you want the event handler to fire after the image has loaded, you must bind it to the image
HTML:
<div id="content">
<!--this stuff takes a long time to load-->
<img id="large" src="http://lorempixel.com/1920/1920/">
</div>
<div id="loading">
<!-- this is the loading gif -->
<img src="http://lorempixel.com/400/200/">
</div>
JS:
// when user browses to page
$('#content').hide();
$('#loading').show();
// then when the #content div has loaded
$('#large').bind('load', function() {
$('#loading').hide();
$('#content').fadeIn('slow');
});
JSFiddle
Or you can bind the event to the window
object, which fires when
the page is fully loaded including graphics.
JS:
$(window).bind('load', function() {
$('#loading').hide();
$('#content').fadeIn('slow');
});
JSFiddle
Yet a third approach, would be to test if all images are loaded, when the load event fires
function allImagesLoaded() {
var imgs = $(this).closest('div').find('img');
var loaded = 0;
imgs.each(function() { if ($(this.complete)) ++loaded; });
if (imgs.length == loaded) {
$('#loading').hide();
$('#content').fadeIn('slow');
}
}
// when user browses to page
$('#content').hide();
$('#loading').show();
// then when the #content div has loaded
$('#content img').bind('load', allImagesLoaded);
JSFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With