The first step is to load a spinner symbol instead of the actual image, but to write the image in the data-src property so that the HTML looks something like this:
<img class="lazy_load_image" src="/static/spinner.gif" data-src="{{ path_to_image }}" alt="{{ image_name }}" style="width:300px;hight:400px;">
Now after page load, we want to load the images and we do this with Javascript by adding an event listener
// This code lazy loads the images in the viewport after the page load window.addEventListener('load', function(){ load_lazy(); }, false)
// This function loads all images in the viewport function load_lazy(){ // we expand the viewport by 200px var images = $('.lazy_load_image').withinviewport({top: -200, bottom: -200}); for(var i = 0; i < images.length; i++){ var image_src = images[i].getAttribute('data-src'); if(image_src){ images[i].setAttribute('src', image_src); images[i].className -= 'lazy_load_image'; } } }
Here I extended the viewport by 200px, just to make sure that the user does not see a spinner forever if less than 50% of the image is in the viewport.
The last step is to make sure that images get loaded if the user scrolls down, which we can do by adding
// This code lazy loads the images in the viewport after scrolling $(window).scroll(function(){ load_lazy(); })
Hope that was useful. I posted the entire code on GitHub.
cheers
Florian
No comments:
Post a Comment