使用 jquery 预加载图像 - 图像是否保留在缓存中?
所以我有一个关于预加载的一般性问题。我使用一些大图像作为精灵附加在整个移动网站的各个位置,但因为它们是背景图像(当jquery将某些类名分配给div时动态分配) - 我不确定如何最好地预加载这些。
我在这里看到这篇文章:
http://jquery-howto .blogspot.com/2009/02/preload-images-with-jquery.html
你只需这样做:
var image1 = $('<img />').attr('src', 'imageURL.jpg');
但如果我以这种方式预加载图像,然后永远不会直接将它们分配给 dom 元素(因为它们将被指定为带有 css 类的背景图像) - 预加载这些图像对我有什么好处吗?
so I have a general question about preloading. I am using a few large images as sprites to be appended in various places throughout my mobile site, but because they are background images (assigned on the fly when jquery assigns certain class names to divs) - I'm not sure how best to preload these.
I saw this post here:
http://jquery-howto.blogspot.com/2009/02/preload-images-with-jquery.html
where you simply do this:
var image1 = $('<img />').attr('src', 'imageURL.jpg');
but if I preload images in this manner, and then never directly assign them to a dom element (because they will be assigned as a background image with a css class) - is preloading these images doing me any good?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您的脚本在
end
运行而不是在 DOM 渲染的中间运行(因为这会阻塞页面),它仍然应该有帮助。当它们被分配时,您将看不到 flickr 效果。只要图像 URL 不改变,就会从缓存中获取图像。
It should still help as long as your script runs at the
end
and not in the middle of DOM rendering (As that'll block the page). You won't see the flickr effect when they get assigned.The image will be picked up from the cache as long as the image URL doesn't change.