Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Lazy Load div background images

As many of you know it is widely used to lazy load images.

Now i want to use this as lazy load div background images.

How can i do that ?

I am currently able to use http://www.appelsiini.net/projects/lazyload that plugin

So i need to modify it in a way that it will work with div backgrounds

Need help. Thank you.

The below part i suppose lazy loads images

$self.one("appear", function() {     if (!this.loaded) {         if (settings.appear) {             var elements_left = elements.length;             settings.appear.call(self, elements_left, settings);         }         $("<img />")             .bind("load", function() {                 $self                     .hide()                     .attr("src", $self.data(settings.data_attribute))                     [settings.effect](settings.effect_speed);                 self.loaded = true;                  /* Remove image from array so it is not looped next time. */                 var temp = $.grep(elements, function(element) {                     return !element.loaded;                 });                 elements = $(temp);                  if (settings.load) {                     var elements_left = elements.length;                     settings.load.call(self, elements_left, settings);                 }             })             .attr("src", $self.data(settings.data_attribute));     } }); 

Jquery plugin lazy load

like image 519
MonsterMMORPG Avatar asked Aug 18 '12 00:08

MonsterMMORPG


People also ask

Should I lazy load images?

You should always use lazy loading for the images below the fold. As we explained, lazy loading will reduce the real and perceived loading time. User experience will benefit from it — and you users will thank you.

Can you lazy load background images?

Browser-level lazy-loading does not apply to CSS background images, so you need to consider other methods if you have background images to lazy-load. Unlike <img> elements which load regardless of their visibility, image loading behavior in CSS is done with more speculation.

Why is my div background-image not showing?

you have to change your path background-image: url('/ximages/websiteheader1. png') ; TO background-image: url('ximages/websiteheader1. png') ; actually, remove the first / from the path of the image.

Can you give a div a background-image?

Say you want to put an image or two on a webpage. One way is to use the background-image CSS property. This property applies one or more background images to an element, like a <div> , as the documentation explains.


1 Answers

First you need to think off when you want to swap. For example you could switch everytime when its a div tag thats loaded. In my example i just used a extra data field "background" and whenever its set the image is applied as a background image.

Then you just have to load the Data with the created image tag. And not overwrite the img tag instead apply a css background image.

Here is a example of the code change:

if (settings.appear) {     var elements_left = elements.length;     settings.appear.call(self, elements_left, settings); } var loadImgUri; if($self.data("background"))     loadImgUri = $self.data("background"); else     loadImgUri  = $self.data(settings.data_attribute);  $("<img />")     .bind("load", function() {         $self             .hide();         if($self.data("background")){             $self.css('backgroundImage', 'url('+$self.data("background")+')');         }else             $self.attr("src", $self.data(settings.data_attribute))          $self[settings.effect](settings.effect_speed);          self.loaded = true;          /* Remove image from array so it is not looped next time. */         var temp = $.grep(elements, function(element) {             return !element.loaded;         });         elements = $(temp);          if (settings.load) {             var elements_left = elements.length;             settings.load.call(self, elements_left, settings);         }     })     .attr("src", loadImgUri ); } 

the loading stays the same

$("#divToLoad").lazyload(); 

and in this example you need to modify the html code like this:

<div data-background="http://apod.nasa.gov/apod/image/9712/orionfull_jcc_big.jpg" id="divToLoad" />​ 

but it would also work if you change the switch to div tags and then you you could work with the "data-original" attribute.

Here's an fiddle example: http://jsfiddle.net/dtm3k/1/

like image 161
fehrlich Avatar answered Sep 30 '22 15:09

fehrlich