为什么图像预加载无效?

发布于 2024-09-08 19:31:45 字数 521 浏览 0 评论 0原文

我的 CMS 项目有一个时尚的 Web 2.0 登录屏幕,它使用 JavaScript 在屏幕上淡入淡出。为什么即使我已经 120% 确定图像已预加载(我在开发工具中使用了资源监视器),当我的登录屏幕出现时,它们仍然需要一秒钟才能显示。彻底破坏了幻想!看看:

http://www.dahwan.info/Melior (链接已损坏)

当您单击“登录”时,屏幕应该使用 75% alpha 1px png 图像淡入黑暗。尽管图像已预先加载,但直到动画完成后才会显示。但是,如果您单击“取消”并再次登录,动画就会流畅、完美。

谁能想到解决这个问题的方法吗?我的 CMS GUI 的其余部分也有它。就好像没有任何图像预加载一样。

感谢您的回答

编辑:是的,我目前正在为 Google Chrome 5.0.375.99 开发此 CMS,稍后添加多浏览器兼容性。抱歉忽略了这一点

My CMS project has a stylish web 2.0 login screen that fades over the screen using javascript. How come that even though I have made 120% sure that images are preloaded (I used the resource monitor in development tools) they still take a second to show up when my login screen appears. It completely destroys the fanciness! Take a look:

http://www.dahwan.info/Melior (link broken)

When you click login, the screen is supposed to fade to dark using a 75% alpha 1px png image. Even though the image is preloaded, it doesn't show up until after the animation is done. However, if you click cancel and log in again, the animation flows smoothly and perfectly.

Can anyone think of a solution to this problem? I'm having it with the rest of my CMS' GUI as well. It's like there was no image preloading what so ever.

Thanks for answers

EDIT: Ah yes, I'm currently developing this CMS for Google Chrome 5.0.375.99, adding multi browser compatibility later. Sorry for leaving that out

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

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

发布评论

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

评论(4

怂人 2024-09-15 19:31:45

我已经想出了解决我的问题的方法。我现在已经在两台不同计算机上的三个浏览器中对此进行了测试,结果完美。简而言之,在 JavaScript 中的图像预加载函数中,我将每个图像添加到 DOM 中不可见的 Div 标记中。

$('#img_preload').append($('<img />').attr('src', img.src));

现在,图像在页面加载时添加到 Dom,根据我的理论,它们驻留在我的低端计算机的内存中,当我的 CMS 需要它们时,它们会立即出现。

感谢您的评论:)

I have come up with a workaround to my problem. I have now tested this in three browsers on two different computers with perfect results. In short, in my image preloading function in javascript, i added each of the images into the DOM in an invisible Div tag.

$('#img_preload').append($('<img />').attr('src', img.src));

The images now being added to the Dom at page load, and according to my theory resting in the memory of my low-end computers, they appears instantly when my CMS needs them.

Thanks for your comments :)

和我恋爱吧 2024-09-15 19:31:45

关于此问题的有用信息:

Codemonkey 解决方案之所以有效,是因为通过将图像放入隐藏的 div 中,浏览器必须将这些图像保留在内存中,并为 div 可见性可能发生的更改做好准备。如果用户需要将 div 的可见性从隐藏更改为阻止,则必须立即完成。这就是为什么仅将所有图像加载到数组中无法正常工作的原因。

A useful information about this problem:

The Codemonkey solution works because, by putting the images in a hidden div, the browser have to keep those images in memory and ready for a possible change of div's visibility. If the user needs to change de visibility of div from hidden to block, it has to be done instantly. This is why just load all images into an array doesn't work properly.

浅浅淡淡 2024-09-15 19:31:45

您也可以将它们预加载到数组中。您的问题可能是由所谓的“垃圾收集”引起的。浏览器在这里查找正在消耗内存的对象,这些对象在屏幕上不再有实例,并且不再被内存中的其他任何内容引用。

不过,如果您将图像预加载到网络时代,则它们应该加载到缓存中。因此,当再次引用时它们仍然应该重新出现。但是,如果没有为这些类型的文件设置足够长的缓存过期时间,图像也可能会消失。

您的问题也可能是特定于浏览器的......我发现最好为预加载的内容创建一个“锚点”,方法是将它们放入图像数组中,然后在需要时使用该数组调用图像而不是图像 URL (URI)。

这是一篇涵盖该主题的简短文章。

https:// web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5214317.html

You could also just preload them into an array. Your problem might be caused by what is known as "garbage collection". This is where the browser looks for objects that are consuming memory which no longer have an instance on the screen and are not being referenced by anything else in memory.

If you preload images into your web age, they should be loaded into the cache, though. So, they should still re-appear when referenced again. However, images can also disappear if the cache expiration is not set for a long enough length of time for these types of files.

Your problem could also be browser-specific.... I have found that it is always best to create an "anchor" for pre-loaded content by placing them into an image array and then using the array to call up the images when needed instead of the image URL(URI).

Here is a quick-and-dirty article that covers this topic.

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5214317.html

风透绣罗衣 2024-09-15 19:31:45

UI 线程一次只能管理一项任务——它将执行 javascript 或更新 UI。如果在预加载图像的代码之前有大量 JavaScript 解析/执行,则可能会导致延迟。

另一个建议是禁用登录链接的可点击性,直到在页面上检测到图像之后。

这样做相当简单:
函数disableBtn(el){
var btn = document.getElementById(el);
var btnId = btn.id;
btn.disabled = true;
}

要重新启用,请设置 btn.disabled = false(在检测到您的图像已添加到 DOM 后)。

The UI thread can only manage one task at a time -- it will either execute javascript or update the UI. If there is a lot of javascript parsing/executing before the code that preloads the image, that might be causing the delay.

Another suggestion is to disable clickability on the login link until after the image has been detected on the page.

To do so is fairly straightforward:
function disableBtn(el){
var btn = document.getElementById(el);
var btnId = btn.id;
btn.disabled = true;
}

To re-enable, set btn.disabled = false (after detecting that your image has been added to the DOM).

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