I want to enable a lazy loading for the contents of my website.
Just like Jquery Image loading http://www.appelsiini.net/projects/lazyload that is valid only for images.
I want to do it for the content (DIV's).
Suppose we have a long page then i want to download the div as they becomes visible.
I will download the content using JSON or PageMethods. But i want the code that will execute the function for loading contents.
So whether we can somehow find this that div is visible only scrolling down.
Means i need to use some scroll events but dont know how.
Any help is appreciated.
Add the loading="lazy" attribute to your inline image. Set a data-src equal to the path of the image you want to load. You can optionally set an src equal to a fallback image. For responsive images, you can add a data-srcset with the list of your assets.
The basic idea of lazy loading is to load images or iframes only when users need to display them: they won't have to wait for all the elements in the page to be loaded and, therefore, can start using the web page sooner.
To lazy load an image, display a lightweight placeholder image, and replace with the real full-size image on scroll. There are several technical approaches to lazy loading images: Inline <img> tags, using JavaScript to populate the tag if image is in viewport. Event handlers such as scroll or resize.
I was looking for this to load advertising from my openX server only when the advertising should be visible. I'm using the iFrame version of openX which is loaded in a div. The answer here put me on my way to solving this problem, but the posted solution is a bit too simple. First of all, when the page is not loaded from the top (in case the user enters the page by clicking 'back') none of the divs are loaded. So you'll need something like this:
$(document).ready(function(){ $(window).scroll(lazyload); lazyload(); });
Also, you'll need to know what defines a visible div. That can be a div that's fully visible or partially visible. If the bottom of the object is greater or equal to the top of the window AND the top of the object is smaller or equal to the bottom of the window, it should be visible (or in this case: loaded). Your function lazyload()
might look like this:
function lazyload(){ var wt = $(window).scrollTop(); //* top of the window var wb = wt + $(window).height(); //* bottom of the window $(".ads").each(function(){ var ot = $(this).offset().top; //* top of object (i.e. advertising div) var ob = ot + $(this).height(); //* bottom of object if(!$(this).attr("loaded") && wt<=ob && wb >= ot){ $(this).html("here goes the iframe definition"); $(this).attr("loaded",true); } }); }
I tested this on all major browsers and even on my iPhone. It works like a charm!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With