[英]Loading CSS background images before normal images?
I have a ruby on rails web app, and in some views i have many heavy images( <img>
) to render .The <img>
are generated in a Helper. 我在Rails Web应用程序上有一个ruby,在某些视图中我要渲染很多沉重的图像( <img>
)。 <img>
是在Helper中生成的。
The problem is that the css is loaded first, then the js, then the heavy images , and then finally the css refereneced background images. 问题在于,首先加载css,然后加载js,然后加载沉重的图像,然后再加载css参考背景图像。
It takes quite a while for all of the heavy images to load, which therefore holds the whole site up. 所有沉重的图像都需要花费相当长的时间才能加载,因此可以支撑整个站点。
I want to load first the css background images then load the other images, as they obviously hold visual structure of the page. 我想先加载CSS背景图像,然后再加载其他图像,因为它们显然拥有页面的视觉结构。
rails version: 2.3.8 滑轨版本:2.3.8
EDIT: Thank you guys, I apologize for not having shared the code earlier. 编辑:谢谢大家,我很抱歉没有更早地共享代码。
I have two models : Categories and Images, each Category has many images. 我有两个模型:类别和图像,每个类别都有很多图像。 In the view i have a list of categories, which is generated by a helper : 在视图中,我有一个类别列表,该列表由助手生成:
categories.each do |cat|
html << "<a href='##{cat.id}' class='mapcat' >#{cat.libelle}</a>"
end
and when I click on the category the images are displayed 当我单击类别时,将显示图像
categories_images.each do |i|
html << "<div id='#{i.id}' class='#{css}'><img src='/images_moi/categories/#{cat.libelle}/#{i.path_file_name}' />"
end
I have css background image associated to the list of category The problem is that the images ( <img>
) is displayed before the css background images of the list of categories. 我有与类别列表关联的css背景图像问题是图像( <img>
)显示在类别列表的css背景图像之前。
We need to assume things because you haven't shared your code. 我们需要做一些假设,因为您尚未共享代码。
Coming to your query, for now you can preload images using jQuery : 来查询一下,现在您可以使用jQuery预加载图像:
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
// Alternatively you could use:
// (new Image()).src = this;
});
}
// Usage:
preload([
'img/imageName.jpg',
'img/anotherOne.jpg',
'img/blahblahblah.jpg'
]);
This saves the loading time of loading images. 这样可以节省加载图像的加载时间。
Use a base64 string. 使用base64字符串。
Example: 例:
background-image: url(data:image/png;base64,*CONVERTED IMAGE DATA HERE*);
Solution: Do not use the img tag. 解决方案: 请勿使用img标签。
My approach is to lazy load the images using jquery and data- tags. 我的方法是使用jquery和data-tag延迟加载图像。 This approach also allows me to choose different images based on device width and spare tablet/mobile users. 这种方法还允许我根据设备的宽度和平板电脑/移动设备的备用用户来选择不同的图像。
<img src="" data-src="/img/graphic-desktop.jpg" data-smallsrc="/img/graphic-smaller.jpg" alt="Graphics" class="lazy" />
<!-- the following would be in your js file -->
$lazy = $('img.lazy');
$(window).load(function(){
// window load will wait for all page elements to load, including css backgrounds
$lazy.each(function(){
// run thru each img.lazy on the page. spare the jquery calls by making $this a variable
$this = $(this);
// if the window is greater then 800px wide, use data-src, otherwise, use the smaller graphic
($(window).width() >= 800) ? $this.attr('src', $this.attr('data-src')) : $this.attr('src', $this.attr('data-smallsrc'));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.