我如何知道 HTML 何时已完全呈现
情况 1:
我加载一个非常大的 HTML 页面,其中包含许多复杂的布局和字体。 该页面将需要一些未知的时间来呈现。
情况 2:
我使用 jquery .html() 函数对我的 DOM 进行重大更改。 修改后的 DOM 将需要一些未知的时间来渲染。
在这两种情况下,我希望能够用微调器覆盖整个屏幕,直到页面完全完成渲染。
在寻找这个问题的答案时,我发现了类似的问题,但答案并不相关。需要明确的是:
我不想知道 DOM 何时准备好。
我不想知道 HTTP 数据何时被获取。
我想知道 DOM 中的所有内容何时完全绘制到屏幕。
设置一些最坏情况超时并不是可接受的解决方案。
我需要一个针对基于 WebKit 的浏览器的解决方案。
case 1:
I load a very large HTML page that includes a lot of complex layout and fonts.
The page will take some unknown time to render.
case 2:
I use jquery .html() function to make significant changes to my DOM.
The modified DOM will take some unknown time to render.
In both cases, I want to be able to cover the whole screen with a spinner until the page has completely finished rendering.
In searching for answers to this question, I have found similar questions asked but the answers are not relevant. To be clear:
I don't want to know when the DOM is ready.
I don't want to know when the HTTP data has been fetched.
I want to know when everything in the DOM has been completely drawn to the screen.
Setting some worst-case timeout is not an acceptable solution.
I need a solution for WebKit based browsers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是一个小点;大多数浏览器在处理 JavaScript 时不会为旋转器设置动画。特别是 IE,其行为非常单线程。
值得为“旋转器”使用不同的非动画设计。像沙漏一样的东西。
考虑一下您何时呈现挑战:为什么不在 $(document).ready 事件中调用的初始化代码之后放置一些内容。对于 IE,它应该最后触发。
Just a small point; most browsers won't animate a spinner whilst they're processing the javascript. Particularly the IEs which behave very single-threaded.
It's worth using a different, non-animated, design for the 'spinner'. Something like an hourglass.
Thought for your when's it rendered challenge: why not put something after your initialisation code which you call in your $(document).ready event. In the case of IEs, it should fire last.
像这样的事情应该会起作用:
Something like this should hopefully work:
就在我的脑海中,你可以做这样的事情:
当然,这有很多错误:
您链接的文章只谈到了 Safari 3。难道我们仍然不能信任 Safari 两个版本之后的 onload 吗?
Just off the top of my head, you could do something like this:
Of course, there's a lot wrong with this:
The article you linked to only talked about Safari 3. Can we still not trust Safari's onload two versions later?
对于情况一,我认为你可以使用:
For case one I think you can use:
为复杂的 HTML 元素设置监听器(可能需要一些未知的时间来渲染),当每个元素加载完毕后,将其标记为完成并对其进行计数,当事件的计数等于元素总数时,我们可以确认该页面已经完全完成渲染。
Set listeners to the complex HTML elements (that may take some unknown time to render), when each of them has loaded, mark it as done and count it, when the event's count equals to the total of elements, we can confirm that the page has completely finished rendering.