jQuery:文档就绪触发对于我的要求来说太早了

发布于 2024-08-17 04:32:17 字数 218 浏览 7 评论 0原文

我正在开发一个基于所有照片的网站。本网站的某些区域需要根据图像尺寸进行计算才能正常工作。我发现文档就绪触发得太早,因此我的图形用户界面表现不稳定。

我删除了文档就绪函数,并将其替换为良好的 'ol window.onload 函数,因为,如果我阅读正确,该函数在图像完全加载之前不会触发。

我的问题是,这会导致任何问题吗?还有我可能错过的其他解决方案吗?

谢谢各位的帮助!!

I am developing a site based all around photos. Some areas of this site require calculations based on image dimensions in order to work correctly. What i have found is that document ready is firing too early and my gui is behaving erratically as a result.

I removed the document ready function and replaced it with the good 'ol window.onload function, since, if i read correctly, this function does not fire until images are fully loaded.

My question is, will this cause any problems? And are there any other solutions that i have missed perhaps?

Thanks for the help guys!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

国际总奸 2024-08-24 04:32:17

您没有理由不能使用 $(window).load() 而不是 $(document.ready()。您关于该函数在图像之前不会触发的说法是正确的完全加载(或加载失败)。

完全依赖 $(window).load() 的缺点是,在某些情况下,很明显您的 JavaScript 没有工作(即 导航点击事件),直到页面上的每个项目都加载完毕。某些用户(通常是网站的高级用户)会非常快速地点击页面,这会降低整体用户体验。

折中办法是在大多数情况下使用$(window).ready() 只将绝对需要的事件放入 $(window).load()

There is no reason you cannot use $(window).load() as opposed to $(document.ready(). You are correct about the function not firing until images are fully loaded (or failed to load).

The drawback of fully relying on $(window).load() are that in some cases it is readily apparent none of your javascript is working (i.e. navigation or click events) until every single item on your page has loaded. Some users, usually a website's power users, click through pages quite rapidly and this detracts from the overall user experience.

The happy medium would be to use $(window).ready() for most situations and only put events inside $(window).load() that absolutely require them.

絕版丫頭 2024-08-24 04:32:17

请尝试以下操作:

$(window).load(function() {
 alert("images are loaded!");
});

请参阅此链接以了解 $(document).ready()$(window).load()

http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/

Try this instead:

$(window).load(function() {
 alert("images are loaded!");
});

See this link for a comparison of $(document).ready() and $(window).load()

http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/

孤独陪着我 2024-08-24 04:32:17

尽管 window.onload 可以满足您的需求,但我建议稍微加快速度:

$("img.load").load(function(){
    alert($(this).width());
});

现在您可以在加载图像时单独处理图像,而不必等待整个元素集。

Altough window.onload would suit your needs, I'd recommend speeding up the things a bit:

$("img.load").load(function(){
    alert($(this).width());
});

Now you can process image individually as quickly as it is loaded and not waiting for the whole set of elements.

紧拥背影 2024-08-24 04:32:17

这是一个非常简单的解决方案:

包含所有图像的 width=""height="" 属性。这将迫使浏览器即使在加载图像之前也正确且到位地渲染所有内容。加载图像时,页面空间中每个图像所需的空间将被适当留出。

就是这样!

如果您这样做,document.ready 将在您的场景中正常工作。发生的情况是浏览器在检索/加载图像之前不知道图像的大小,因此浏览器必须猜测。这可能会导致有趣的行为。指定图像的宽度和高度将解决这个问题。

我很惊讶没有人提到这一点。尽管cballou的回答绝对是一个很好的建议,但这不一定是一个javascript问题。

不管怎样,希望你学到一些新东西。 ;)

Here’s a very simple solution:

Include the width="" and height="" attributes for all images. This will force the browser to render everything correctly and in place even before images are loaded. The space required for each image in the page's real estate will be properly set aside while images load.

That’s it!

If you do that, document.ready will work just fine in your scenario. What is happening is that the browser doesn't know the size of the images before they are retrieved/loaded, so the browser has to guess. That can cause the funny behavior. Specifying the width and height of your images will solve this problem.

I’m surprised no one mentioned that. It is not necessarily a javascript problem, although cballou's answer is definitely good advice.

Anyway, hope you learned something new. ;)

花开柳相依 2024-08-24 04:32:17

您可以使用 jquery 中的 load() 事件,但是在文档中提到,如果元素已经加载,它可能无法按预期工作。

注意:只有在元素完全加载之前设置它,加载才会起作用,如果在元素完全加载之后设置它,则不会发生任何事情。这不会发生在 $(document).ready() 中,jQuery 会处理它以按预期工作,在 DOM 加载后设置它时也是如此。
-- http://docs.jquery.com/Events/load

You could possible use the load() event in jquery, however in the documentation it is mentioned that it might not work as expected if the element already has loaded.

Note: load will work only if you set it before the element has completely loaded, if you set it after that nothing will happen. This doesn't happen in $(document).ready(), which jQuery handles it to work as expected, also when setting it after the DOM has loaded.
-- http://docs.jquery.com/Events/load

失退 2024-08-24 04:32:17

如果你想延迟一段给定的时间,也许你可以使用“setTimeout”:)

If you want to delay it with an amount of time given maybe you can use "setTimeout" :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文