admin管理员组

文章数量:1322869

I am designing a website for a client and am trying to figure out how to tell the current browser viewing the page to wait until the background image is fully loaded before it is displayed on the page. It is a large background image (bytes-wise) and would like it to load once loaded. I also want to do the exact same with the logo, but am stumped.

To be quite honest with all of you, I know very little JavaScript and jQuery, which is why I ask, please someone help me it would make the page a lot more attractive. The reason why I stated that above is because I know this may only be done in JavaScript.

I am designing a website for a client and am trying to figure out how to tell the current browser viewing the page to wait until the background image is fully loaded before it is displayed on the page. It is a large background image (bytes-wise) and would like it to load once loaded. I also want to do the exact same with the logo, but am stumped.

To be quite honest with all of you, I know very little JavaScript and jQuery, which is why I ask, please someone help me it would make the page a lot more attractive. The reason why I stated that above is because I know this may only be done in JavaScript.

Share Improve this question edited May 17, 2011 at 0:47 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked May 17, 2011 at 0:09 Aaron BrewerAaron Brewer 3,67719 gold badges51 silver badges79 bronze badges 2
  • 1 I'm thinking a div that has onload="divID.setVisible(true);" might do it. – user684934 Commented May 17, 2011 at 0:14
  • @bdares Where would I play that set up code? <script type="text/javascript"> CODE HERE </script> between the <head> tag(s)? And also the background image is called via the body. Thank you. – Aaron Brewer Commented May 17, 2011 at 0:20
Add a ment  | 

2 Answers 2

Reset to default 5

What es to mind without actually using or testing 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./ */
.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');
    });
});

Put this at the bottom of your HTML document (before closing body tag). Obviously, the URL should be your image's URL.

<script type='text/javascript'>
  $(document).ready(function() {
    $('body').css('backgroundImage', 'url(http://yoursite/yourimage.jpg)');
  });
</script>

And you have to import jQuery library in the head of your document (or you can download jQuery and put it on your server).

<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

Also, if you don't want to attach background image to body, just put your div's id instead (like ('#content')).

本文标签: javascriptWait for image to be fully loaded before displayed on pageStack Overflow