最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How can I make sure a background image displays before the page loads? - Stack Overflow

programmeradmin5浏览0评论

Is there a way to make sure a (large, 300K) background picture is always displayed first BEFORE any other content is shown on the page?

On the server we have access to PHP.

Is there a way to make sure a (large, 300K) background picture is always displayed first BEFORE any other content is shown on the page?

On the server we have access to PHP.

Share Improve this question asked Oct 14, 2008 at 10:30 Edward TanguayEdward Tanguay 193k320 gold badges725 silver badges1.1k bronze badges 1
  • I'd suggest making the image smaller, 300k is rather large for the background of a site imo. – Henry B Commented Oct 14, 2008 at 11:20
Add a ment  | 

6 Answers 6

Reset to default 3

All the html content is served and parsed before it even starts to fetch the image, so you have a problem before you start.

You could circumvent this by programmatically hiding the content, and then triggering a "show" of it when the image is loaded.

ie:

<html>
  <body>
    <image here/>
    <div id="content" style="display:none;" >

    </div>
    <script type="psudocode"> 
     when(image.loaded){ 
          $("#content").show();
     }
    </script>
  </body>
</html>

If you have all content inside a container, you can probably e pretty close using this techique. It will also fail gracefully if javascript is disabled/unavailable.

So if you have to do this because of a manager or something, this is the method I would use.

<html><head><!-- head section --></head>
<body>
    <div id="container">
       <script type="text/javascript">
       <!--
         document.getElementById('container').style.display = 'none';
       -->
       </script>
       Content goes here
    </div>
    <script type="text/javascript">
    <!--
      document.getElementById('container').style.display = 'block';
    -->
    </script>
</body></html>

If you have very little content, however, it probably won't do much good. You could of course add a timer on the second javascript block, to delay it for a second or so :P

<html><head>
<script type="text/javascript">
//Hide on load
onload=function(){
    document.body.style.display="none";
    var imag = new Image();

    imag.onload=function(){
        var main = document.body;
        main.style.backgroundImage="url("+this.src+")";
        main.style.display="";

    };
    imag.src="http://dayton.hq.nasa.gov/IMAGES/MEDIUM/GPN-2000-001935.jpg";
}
</script>
</head>
<body>
    YOU CAN' T SEE MEE!!
</body>
</html>

A possible way, if you don't want to rely on JavaScript, is to make a dummy page with only the background image. After a few seconds, it redirects to the real page, and the background will load quickly because it is already in cache.

Not a super attractive solution, if the timing is fixed, I reckon.
Note that 300KB is quite big for a background image. I have seen worse, somebody using a 1MB image: even with a relatively fast connexion, I could see the background load way after the elements of the page.

I think the only way you'll be able to do this is with javascript - Send the user HTML that only contains your background image and some javascript that either waits for a certain amount of time before displaying the rest of the content or uses AJAX to retrieve the rest of the content (essentially the same thing).

You could load the image within the page as a base64 image then it will already be loaded with the page.

发布评论

评论列表(0)

  1. 暂无评论