简体   繁体   English

隐藏页面直到所有内容加载完毕

[英]Hide page until everything is loaded Advanced

I have a webpage which heavily makes use of jQuery.我有一个大量使用 jQuery 的网页。

My goal is to only show the page when everything is ready.我的目标是仅在一切准备就绪时显示页面。

With that I want to avoid showing the annoying page rendering to the user.因此,我想避免向用户显示烦人的页面呈现。

I tried this so far ( #body_holder is a wrapper inside body ):到目前为止我试过了( #body_holderbody内的包装器):

$(function(){
    $('#body_holder').hide();
});
$(window).load(function() {
    $("#body_holder").show();
});

This works completely fine, but messes up the layout.这完全可以正常工作,但会弄乱布局。

The problem is that hiding the wrapper interferes with the other jQuery functions and plugins used (eg layout-plugin).问题是隐藏包装器会干扰其他 jQuery 功能和使用的插件(例如布局插件)。

So I guess there must be another trick to do this.所以我想一定有另一个技巧可以做到这一点。 Maybe lay a picture or div over the body until window.load has occurred?也许在身体上放一张图片或 div 直到 window.load 发生?

What approaches do you use?你使用什么方法?

EDIT:编辑:

The solution most likely has to be another way than display:none or hide() ;解决方案很可能必须是display:nonehide()之外的另一种方式;

Anything done with jQuery will normally have to wait for document.ready, which is too late IMHO. 使用jQuery做的任何事情通常都要等到document.ready,这已经太晚了恕我直言。

Put a div on top, like so: 将div放在顶部,如下所示:

<div id="cover"></div>

set some styles: 设置一些样式:

#cover {position: fixed; height: 100%; width: 100%; top:0; left: 0; background: #000; z-index:9999;}

and hide it with JS when all elements are loaded: 并在加载所有元素时用JS隐藏它:

$(window).on('load', function() {
   $("#cover").hide();
});

Or if for some reason your script uses even longer time then the DOM elements to load, set an interval to check the type of some function that loads the slowest, and remove the cover when all functions are defined! 或者,如果由于某种原因你的脚本使用更长的时间然后加载DOM元素,设置一个间隔来检查加载最慢的某个函数的类型,并在定义所有函数时删除封面!

 $(window).on('load', function() { $("#cover").fadeOut(200); }); //stackoverflow does not fire the window onload properly, substituted with fake load function newW() { $(window).load(); } setTimeout(newW, 1000); 
 #cover {position: fixed; height: 100%; width: 100%; top:0; left: 0; background: #000; z-index:9999; font-size: 60px; text-align: center; padding-top: 200px; color: #fff; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li>This</li> <li>is</li> <li>a</li> <li>simple</li> <li>test</li> <li>of</li> <li>a</li> <li>cover</li> </ul> <div id="cover">LOADING</div> 

Here is a jQuery solution for those looking: 这是一个jQuery解决方案,适合那些寻找:

Hide the body with css then show it after the page is loaded: 使用css隐藏正文,然后在加载页面后显示它:

CSS: CSS:

html { visibility:hidden; }

JavaScript JavaScript的

$(document).ready(function() {
  document.getElementsByTagName("html")[0].style.visibility = "visible";
});

The page will go from blank to showing all content when the page is loaded, no flash of content, no watching images load etc. 当页面加载时,页面将从空白变为显示所有内容,没有内容闪烁,没有观看图像加载等。

You should try setting visibility to hidden instead of display:none. 您应该尝试将可见性设置为隐藏而不是display:none。 Setting visibility to hidden will retain all elements positions and dimensions, thus it shouldn't create layout problems. 将可见性设置为隐藏将保留所有元素位置和尺寸,因此不应创建布局问题。

Stumbled upon this and tried @9ete's solution but it didn't help me.偶然发现并尝试了@9ete 的解决方案,但它对我没有帮助。 This worked instead:这反而奏效了:

CSS: CSS:

html { visibility:hidden; }

JS:记者:

window.addEventListener('load', function () {
    document.getElementsByTagName("html")[0].style.visibility = "visible";
});

As per documentation for window , the load event is fired after all the content (images included) is loaded while $document says that ready is fired after only the DOM is ready.根据window的文档,在加载所有内容(包括图像)后会触发load事件,而$document表示仅在 DOM 准备ready后才会触发 ready 事件。

Your question is valid, but I would not get in a practice of hiding or covering the page while things are spinning up. 你的问题是有效的,但是当事情发生时我不会隐藏或覆盖页面。

It keeps the user from understanding what's happening on the page. 它使用户无法理解页面上发生的事情。 While lots of things may need to load, different parts of the page should spring to life as they're loaded. 虽然可能需要加载很多东西,但页面的不同部分应该在加载时生动。 You should get in the practice of locking controls that are not ready, perhaps displaying a spinner or some other progress indicator over them. 您应该参与锁定尚未就绪的控件,可能会在其上显示微调器或其他一些进度指示器。 Or setting the cursor to wait on loading items. 或者将cursor设置为wait加载项目。

This keeps the user in the loop and allows him to see and interact with parts as they come online instead of obscuring all parts until everything is ready. 这使用户处于循环中,并允许他在部件上线时查看和交互部件,而不是模糊所有部件,直到所有部件都准备就绪。

You will normally want to load the things the user needs the quickest access to, usually stuff above the fold, first. 您通常希望首先加载用户需要最快访问的内容,通常是首屏。 Loading is a prioritization that can easily be coordinated with Promises. 加载是一个可以轻松与Promises协调的优先级。

At the very least seeing the page allows the user to get his bearings and decide what to do. 至少看到页面允许用户获得他的方位并决定做什么。 Be transparent. 要透明。

I was seeking a non-javascript solution so I found one that is working on most browsers in acceptable manner. 我正在寻找一个非JavaScript的解决方案,所以我找到了一个以可接受的方式在大多数浏览器上运行的解决方案。

Since the loading order of CSS rules matters; 由于CSS规则的加载顺序很重要;

  • Define the hiding class in the first CSS file or inline in head. 在第一个CSS文件中定义隐藏类或在头部内联。
.hidden-onpage-load{ display: none; } 
  • In the body, the class can be used as 在体内,该类可以用作
<div class="hidden-onpage-load"> ... </div>
  • Redefine it inline or in a CSS file after all other CSS and JS files are loaded 在加载所有其他CSS和JS文件后,重新定义内联或CSS文件
.hidden-onpage-load{ display: block; } 

Start your HTML with: 用以下内容启动HTML:

<body style="opacity:0;">

At the end of your script: 在脚本的最后:

document.body.style.opacity = 1;

The simplest solution I've come up with is to wrap the body in a as suggested previously, but set it as hidden from the get go, then use JQuery (or javascript) to unhide on load after all components are loaded. 我提出的最简单的解决方案是按照之前的建议将主体包装起来,但是在开始时将其设置为隐藏,然后在加载所有组件后使用JQuery(或javascript)取消隐藏。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="bodyDiv" hidden> Hello World! </div> <script> $(document).ready(function() { // add JQuery widget loads here $("#bodyDiv").show(); // reveal complete page }) </script> 

Don't forget, a lot of frameworks use javascript to structure a page.不要忘记,很多框架使用 javascript 来构建页面。 To prevent the page from showing before these modification have been made you'll need to do something like what is described here (eg run a script at the end of the page to show the real contents of the page):为了防止页面在进行这些修改之前显示,您需要执行此处描述的操作(例如,在页面末尾运行脚本以显示页面的真实内容):

Detect if any JavaScript function is running 检测是否有JavaScript function正在运行

If you have a div #bodyholder then you can put display:none in your CSS for it and then with jQuery do: 如果你有一个div #bodyholder,那么你可以在你的CSS中放置display:none,然后使用jQuery do:

$(document).ready(function() {
     $('#body_holder').show();
});

I don't see why hiding a div should interfere with the loading of anything, because all it means is it is hidden. 我不明白为什么隐藏div应该干扰任何东西的加载,因为它意味着它是隐藏的。 However, if you have lots of jQuery being used then make sure you wrap it in $(document).ready which will make sure that the DOM is fully loaded before the Javascript is executed 但是,如果您使用了大量的jQuery,请确保将其包装在$(document).ready中,以确保在执行Javascript之前完全加载DOM

A further point is that HTML/CSS is designed for progressive loading, and if you do it properly then you can get a nice progressive loading of content for your users. 另外一点是HTML / CSS是为渐进式加载而设计的,如果你正确地进行了加载,那么你可以为用户提供一个很好的渐进式内容加载。 I personally wouldn't want my users getting a white screen for a few seconds until everything was loaded. 我个人不希望我的用户在加载所有内容之前几秒钟都会看到白屏。 Move your Javascript to the end of the page so that it doesn't block loading and get content onto the screen as quickly as possible. 将您的Javascript移动到页面末尾,以便它不会阻止加载并尽快将内容添加到屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM