为元素预加载 js
有一个div #container
,其中包含带有大背景图像的各种div,
我想使用一些Pre-Image loading js
来避免加载图像的丑陋外观。
但问题是我找不到任何在后台加载所有图像的脚本,而是在 #container
中显示加载栏。
尽管互联网上充满了加载全身图像的脚本,但我只想加载一个元素,同时其他所有内容(例如内容)都是可见的。
更新的问题: 以下脚本适用于 CSS 中指定的背景图像吗?
$('#container img:last').load(function(){
//do stuff
});
更新:最好的解决方案是使用这个 jQuery 插件,它称为“preloadCssImages”。您可以在此处找到它。
它预先加载所有存在的 css 图像,甚至不需要做所有这些事情。
There is a div #container
which contains various divs with large background images,
I want to use some Pre-Image loading js
for avoiding the ugly look of loading image.
But the problem is that I can't find any script which loads all images in background and instead in #container
display a loading bar.
Though Internet is full of scripts which loads images for whole body but I want just for an element meanwhile everything else is visible such as content.
UPDATED Question:
Would the following script work for the background images specified in CSS?
$('#container img:last').load(function(){
//do stuff
});
UPDATE:Best solution is get hold of this jQuery plugin, its called "preloadCssImages". You can find it here.
It pre loads all the css images present without even bothering to do this all stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以等待容器中的最后一个图像加载,然后执行您的操作。
所以像这样
$('#container img:last').load(function(){
//做事
});
You can wait for the last image in the container to load then perform your actions.
so something like this
$('#container img:last').load(function(){
//do stuff
});
首先在容器内设置加载图像:
当所有图像加载完成后,清空 #container div 并将所有图像附加到 div 中,如下所示:
First set a loading image within the container:
When all images load complete, then empty #container div and append all images into the div like this:
一个优雅的解决方案是使用 CSS 级联。 HTML:
CSS:
JS(加载时或准备好时):
大多数浏览器仅根据需要加载未使用的图像,或在触发窗口加载事件后加载它。然而,图片也是按照遇到的顺序放入加载队列的,所以即使上面的JS是在DOM准备好时执行的,它仍然会在.loadimage类后面没有级联的所有其他图片之后加载图片。
An elegant solution is to use CSS cascading. HTML:
CSS:
JS (on load or whenever you're ready):
Most browsers only load unused images as needed, or load it after the window load event is fired. However, images are also put in the load queue in the order that they are encountered, so even if the JS above is executed on DOM ready, it will still load the images after all the other images that are not cascaded behind the .loadimage class.