简体   繁体   English

html,仅在等待图像时使页面显示就绪(没有选项卡旋转轮)

[英]html, make page appear ready (no tab spinning wheel) while just waiting for images

I have a page that loads images from various sources. 我有一个页面可以加载来自各种来源的图像。 Occasionally these images fail to load; 有时,这些图像无法加载; perhaps the link has gone dead or whatever. 也许链接已失效或其他原因。 That's fine. 没关系。

What bothers me is that the browser might take 6 seconds or even longer (I've seen 20 seconds) before it decides that the image has failed to load. 让我困扰的是,浏览器可能需要6秒钟甚至更长的时间(我看过20秒钟)才能确定图片无法加载。 During this time the spinning loading wheel in the Chrome tab keeps going and going, making it seem like my page isn't ready. 在这段时间内,Chrome标签页中不断旋转的加载轮不断旋转,似乎我的页面尚未准备好。

I've switched my javascript loading from onload to $(document).ready() so at least my page isn't inactive while it waits for the images to load. 我已将JavaScript加载从onload切换为$(document).ready(),因此在等待图像加载时,至少我的页面处于非活动状态。 But it might appear as though it is. 但是看起来好像是这样。

But is there some way to make my page appear "ready" (no spinning wheel) when all it's doing is waiting for the image? 但是,当所有图片都在等待图像时,是否可以通过某种方式使我的页面显示为“就绪”(没有旋转轮)? Maybe another way to load images? 也许是加载图像的另一种方法? I currently use the img element with src. 我目前将img元素与src一起使用。 Or a way to make it give up sooner? 还是让它早日放弃的方法? Does it really need 6 seconds to decide that an image link is dead? 确定图像链接已失效真的需要6秒钟吗?

Not sure if this has a solution. 不知道这是否有解决方案。 It's a problem that I have seen on a lot of websites, not just mine, but it drives me nuts. 我在很多网站上都看到过这个问题,不仅是我的,而且让我发疯。 I'll often click the stop-loading-x just to make it stop! 我会经常单击stop-loading-x来使其停止! I'd at least like for my own website to not be like that. 我至少希望自己的网站不是那样。

According to my tests, the loading indicator in Chrome does not show for image elements where the loading was triggered by Javascript. 根据我的测试,Chrome中的加载指示器未显示由Javascript触发加载的图像元素。 Thus, if you don't mind the images not loading with javascript disabled, you could send the img with the src unset, and set it when the page loads. 因此,如果您不介意在禁用javascript的情况下无法加载图片,则可以发送未设置srcimg ,并在页面加载时进行设置。 You can store the URL in data-src . 您可以将URL存储在data-src The upside is then that you can control when the image loads, if you want to (though you may want to use a plugin for that, like Roullie's answer suggests). 有利的一面是,然后, 图像加载,如果你愿意,你可以控制(虽然你可能想使用插件,像Roullie的答案建议)。


<img width=100 height=100 class="async-img" data-src="http://www.example.com/some.png">

... ...

$(document).ready(function(){
  $(".async-img").each(function(){
    this.src = $(this).data("src");
  })
})

maybe it can help. 也许可以帮上忙。 lazyload.js lazyload.js

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

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