Lazy Loading in Flexslider
Asked Answered
R

6

17

I am trying to get lazy loading working for Flexslider by using Lazy Loading jquery plugin and following the instructions on this site: http://www.appelsiini.net/projects/lazyload

I am loading the plugin script and don't see any errors on console. I tried without the container or options being passed in lazyload function and still nothing. I spend hours on this.

$("img.lazy").lazyload({
  effect: "fadeIn",
  container: $(".slides > li")
});

Does anyone know how to get lazy loading working in Flexslider?

Rochellerochemont answered 10/9, 2012 at 22:52 Comment(0)
R
0

So I added the real image page to the data-attr attribute on the image tag and on after event fire I would find the image with active class and set the img src attribute equal to data-attr value.

Rochellerochemont answered 19/9, 2012 at 0:58 Comment(2)
Do you have an example? Or could you throw one up in js fiddle?Caecum
As @J0NNYZER0 says, could you put a jsfiddle up of your solution? Thx.Limestone
O
15

I implemented a lazy load during scrolling. This solution works better for big collections in comparison with other solutions proposed here. During initialization it loads only first 5 images and then it loads ahead 3 images for every animation.

<li>
  <img class="lazy" src="loading-image.jpg" data-src="actual-image.jpg" />
</li> 

and javascript code:

    $('.flexslider').flexslider({
        animation: "slide",
        animationLoop: false,
        controlNav: false,
        init: function (slider) {
            // lazy load
            $("img.lazy").slice(0,5).each(function () {
                var src = $(this).attr("data-src");
                $(this).attr("src", src).removeAttr("data-src").removeClass("lazy");
            });
        },
        before: function (slider) {
            // lazy load
            $("img.lazy").slice(0,3).each(function () {
                var src = $(this).attr("data-src");
                $(this).attr("src", src).removeAttr("data-src").removeClass("lazy");
            });
        }
    });
Olympus answered 5/2, 2014 at 21:11 Comment(4)
Can you send a JSFiddle or a Codepen for this bro?Retrograde
@RomanPodlinov how can i use this concept to ResponsiveSlider ??Pammie
@Vikram sorry, I didn't work with ResponsiveSlider, but IMO the concept must work too. You just have to bind lazyload on the events from the sliderOlympus
Still using flexslider in 2020. Great answer, simple concept, works as intended. I think this should be the accepted answer. The accepted answer hints at the same concept, but this answer is complete.Grownup
O
8

The method works pretty well for me, but the loading image does need to be the same size as the rest of the images. I actually use http://imageresizing.net/ with mode=pad

In the main container that you are using for flexslider, put the actual image in a "data-src" attribute

<li>
  <img class="lazy" src="loading-image.jpg" data-src="actual-image.jpg" />
</li>            

In you initialization function, use the "start" callback to replace the loading image with the actual image, and remove the attribute

$('#slider').flexslider({
    start: function (slider) {
       // lazy load
       $(slider).find("img.lazy").each(function () {
          var src = $(this).attr("data-src");
          $(this).attr("src", src).removeAttr("data-src");
       });
     }
});

I hope this helps someone.

Orography answered 28/10, 2012 at 17:35 Comment(2)
It works for me, I make only one improvement instead of adding class="lazy" directly, I search for img with attribute "data-src" $(slider).find("img[data-src]").each(function () ...Nyaya
This helped me most noble sire. I'm much indebted to yeArchlute
O
4

I am trying to do the same thing using Flexslider 2 and the Lazy Load Plugin for jQuery.

It seems the container property only works if the element is styled with overflow:scroll;

I was able to get the lazy load to work using this code:

$("#flexcontainer img.lazy").lazyload({
    failure_limit : 10,
    effect: "fadeIn",
    skip_invisible : false
});

However, this just lazy loads everything at once instead of as the flexslider animates.

I was also able to get it to work on mouse over using this code:

 $("#flexcontainer img.lazy").lazyload({
     failure_limit : 10,
     effect: "fadeIn",
     event : "mouseover"
 });

However this doesn't work on touch devices.

I think the key is to create your own custom event and have it trigger after a flexslider callback such as the after callback.

Oath answered 14/9, 2012 at 20:56 Comment(2)
Thanks for the answer but that is what I did. I implemented my our lazy loading logic based on what lazy load library does on after event of flexslider that is triggered after the slide is done.Rochellerochemont
You lazy load solution doesn't make any sense for big image collections due to it loads ALL images. I proposed better solution below.Olympus
G
2

For the sake of offering an alternative solution - another option is to use a responsive slider which already has lazy load built into it, for example royal slider, here is the link -> http://dimsemenov.com/plugins/royal-slider/

Greenman answered 27/9, 2013 at 19:20 Comment(0)
E
1

You might as well initialize lazyload with a custom trigger event ...

$jQ("img[data-original]").lazyload({
            effect: "fadeIn",
            skip_invisible: false,
            event: "forceLazyLoad"
        });

... and then call this event to preload all your images inside the flexslider with a call like:

$jQ('.flex-viewport').find('img[data-original]').trigger('forceLazyLoad');
Ellary answered 5/8, 2013 at 16:54 Comment(0)
R
0

So I added the real image page to the data-attr attribute on the image tag and on after event fire I would find the image with active class and set the img src attribute equal to data-attr value.

Rochellerochemont answered 19/9, 2012 at 0:58 Comment(2)
Do you have an example? Or could you throw one up in js fiddle?Caecum
As @J0NNYZER0 says, could you put a jsfiddle up of your solution? Thx.Limestone

© 2022 - 2024 — McMap. All rights reserved.