延迟加载某个CSS背景图片

发布于 2024-07-13 06:38:12 字数 645 浏览 8 评论 0原文

在这样的 CSS 中:

...
#big-menu {
    background: #fff url(../images/big-menu.jpg) no-repeat;
}
#some-menu {
    background: #fff url(../images/some-menu.jpg) no-repeat;
}
#some-other-menu {
    background: #fff url(../images/some-other-menu.jpg) no-repeat;
}
...

有没有办法延迟 #big-menu 的背景图像的加载,以便它在其他所有内容之后加载,包括 HTML 中的所有图像,以及每个其他 CSS 背景(some-menusome-other-menu)。

原因是,big-menu.jpg 非常大,我希望它最后加载。 毕竟,它只是起到养眼的作用,还有其他背景图像比它有更好的用途。 (例如按钮中使用的)

将其放入 CSS 的顺序或 HTML 中标记 (#big-menu) 的出现是否与首先加载的内容有关? 或者有更可靠的方法来控制它? javascript(首选 jQuery)就可以了。

in a CSS like this :

...
#big-menu {
    background: #fff url(../images/big-menu.jpg) no-repeat;
}
#some-menu {
    background: #fff url(../images/some-menu.jpg) no-repeat;
}
#some-other-menu {
    background: #fff url(../images/some-other-menu.jpg) no-repeat;
}
...

is there a way to delay the loading of #big-menu's background image, so that it loads after everything else, including all of the images in the HTML, and every other CSS background (the some-menu and some-other-menu).

The reason is, big-menu.jpg is very heavy in size, and I want it to be loaded last. After all, it just serves as an eye-candy, and there are other background images that have a better use than that. (such as the ones used in buttons)

Does the order of putting it in CSS or the occurrences of the markup (#big-menu) in HTML got anything to do with what gets loaded first ? or is there a more reliable way to control it ? javascript (jQuery preferred) is fine.

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

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

发布评论

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

评论(7

坚持沉默 2024-07-20 06:38:12

css 中的顺序不控制首先加载哪些资源,但会影响覆盖或继承哪些现有样式。

尝试 JavaScript onload 事件。 该事件仅在页面完成加载后执行。

function loadBG(){
    document.getElementById("big-menu").style.backgroundImage="url(../images/big-menu.jpg)";
}
window.onload=loadBG();

The ordering in css doesn't control which resources are loaded first, but do affect what existing style is being overwritten or inherited.

Try the JavaScript onload event. The event executes only after the page is finish loading.

function loadBG(){
    document.getElementById("big-menu").style.backgroundImage="url(../images/big-menu.jpg)";
}
window.onload=loadBG();
把昨日还给我 2024-07-20 06:38:12

这可能不是一个完美的解决方案(因为它是 css 背景而不是图像),但是 YUI图像加载器提供了一种延迟加载图像的好方法。

创建组

var imgGroup = new YAHOO.util.ImageLoader.group(window, 'load', 2);

文档加载后,图像将在其后 2 秒加载。 请参阅雅虎! Shine 或演示的示例

This might not be a perfect solution(as it is a css-background and not a image), but YUI Image loader provides a good way of lazy-loading images.

Create a group

var imgGroup = new YAHOO.util.ImageLoader.group(window, 'load', 2);

After the document has loaded, the image will load 2 seconds after it. See Yahoo! Shine or the examples for demo.

老街孤人 2024-07-20 06:38:12

另一种解决方案是将您的资产分割到不同的域中,浏览器一次只会请求两个资产,然后等待一个资产加载,然后再开始下一个。 您可以使用 FirebugYSlow,因此将一些资源放在 images.example.com 和 images1.example.com 上,看看是否会产生影响

Another solution is to split your assets over different domains, browsers will only request two assets at time then wait for one to be loaded before starting the next. You can observe this with Firebug and YSlow, so placing some assets on images.example.com and images1.example.com and see if that makes an impact

迟月 2024-07-20 06:38:12

我认为样式表中的放置不会影响它,因为应用它们的元素的顺序将决定首先加载哪个图像。

您可以使用 jQuery 以正确的顺序加载图像,但

$('firstElement').addClass('some-menu');
$('secondElement').addClass('some-other-menu');
$('lastElement').addClass('big-menu');

编辑:
好的,那么也许更好的解决方案是在运行时添加背景图像,而不是添加类。

$('firstElement').css("background-image", "some-menu.jpg");
$('secondElement').css("background-image", "some-other-menu.jpg");
$('lastElement').css("background-image", "big-menu.jpg");

I don't think placement in the styelsheet will affect it, as the order of the elements to which they are applied will decide which image will be loaded first.

You could use jQuery to load the images in the right sequence, though

$('firstElement').addClass('some-menu');
$('secondElement').addClass('some-other-menu');
$('lastElement').addClass('big-menu');

EDIT:
Okay, so then maybe instead of adding a class, a better solution would be to add the background-images in runtime.

$('firstElement').css("background-image", "some-menu.jpg");
$('secondElement').css("background-image", "some-other-menu.jpg");
$('lastElement').css("background-image", "big-menu.jpg");
治碍 2024-07-20 06:38:12

在 CSS 中控制图像加载时间实际上是不可能的。 您可以尝试将其放在样式表的最底部,但我怀疑您会看到改进。

Controlling when an image loads in CSS is not really possible. You could try putting it at the very bottom of your StyleSheet but I doubt you'll see an improvement.

乖乖兔^ω^ 2024-07-20 06:38:12

使用css无法控制下载的顺序。 不过你可以使用 javascript 来实现这一点。

虽然与问题没有直接关系,但也许在这样做之前,您应该使用 smush.it 或您最喜欢的图像程序来优化图像,但我发现以 85% 质量保存时,jpeg 看起来非常好。 进行实验,不要忘记将您的网络服务器设置为长时间缓存图像,然后问题将只是您的用户第一次访问该网站。

The order of downloading can't be controlled by using css. However you can use javascript to achieve this.

Although not directly related to the question maybe before doing that you should optmize the image by using smush.it or your favorite image program I find that jpegs look very good when saved at 85% quality. Experiment and don't forget to set your web server to cache images for long periods and then the problem will just be the first time your users go to the site.

徒留西风 2024-07-20 06:38:12

Jason 的 Javascript 技巧帮助我解决了这个问题。 但是,我必须稍微纠正一下语法:

function loadBG(){
     document.getElementById("big-menu").style.backgroundImage="url(../images/big-menu.jpg)";
}
window.onload=loadBG();

Jason's Javascript trick worked for me in solving this problem. However, I had to correct the syntax slightly:

function loadBG(){
     document.getElementById("big-menu").style.backgroundImage="url(../images/big-menu.jpg)";
}
window.onload=loadBG();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文