在页面加载时隐藏文档正文 [英] Hide document body on page load

查看:184
本文介绍了在页面加载时隐藏文档正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载在文档头中的javascript,需要隐藏文档正文,直到所有页面内容加载。我试过:

I have a javascript that is loaded in the document head that needs to hide the document body until all page contents have loaded. I have tried:

$(document.body).hide();

但在某些浏览器中似乎仍然闪烁。我正在使用最新的jQuery库(1.6.2)。在domready上触发事件会隐藏文档正文,但也会导致闪烁。

But there still appears to be a flicker in some browsers. I am using the latest jQuery library (1.6.2). Firing the event on domready does hide the document body, but causing a flicker as well.

我可以控制的是javascript中的内容。作为一个插件,我无法操纵静态html页面。

All that I can control is what is in the javascript. I cannot manipulate the static html pages as the script is being developed as a plugin.

推荐答案

最好的方式是将所有内容放在容器div中,并具有默认隐藏它的样式表。一旦所有的东西都加载,你可以显示内容。在默认页面内容呈现之前,无法运行Javascript,因此开始隐藏的唯一方法是使用静态定义的CSS规则:

The best way to do this is to put all content in a container div and have a style sheet that hides it by default. You can then show the content once everything is loaded. There is no way to run Javascript before the default page content renders so the only way to start out hidden is with a statically defined CSS rule:

HTML:

<body>
    <div id="container">
        all dynamic page content goes here
    </div>
</body>

样式表中的CSS与该页面最初不可见:

CSS in a stylesheet with the page to make it initially not visible:

#container {display: none;}

然后,您可以使用javascript进行任何所需的操作,当您完成页面构建时,您可以执行此操作使其可见:

And, then you can do whatever you want with javascript and when you're done building the page, you do this to make it visible:

document.getElementById("container").style.display = "block";

或在jQuery中:

$("#container").show();

这篇关于在页面加载时隐藏文档正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆