I think that the author meant to additionally connect to the whileScrolling event as follows:
(function($){ $(window).load(function(){ $("#content_1").mCustomScrollbar({ scrollButtons:{ enable:true }, callbacks:{ whileScrolling:function(){ WhileScrolling(); } } }); function WhileScrolling(){ $("img.lazy").lazyload(); } }); })(jQuery);
where the HTML container is as follows:
<div id="content_1" class="content"> <p>Lorem ipsum dolor sit amet. Aliquam erat volutpat. Maecenas non tortor nulla, non malesuada velit.</p> <p> <img class="lazy img-responsive" data-original="/img/bmw_m1_hood.jpg" width="765" height="574" alt="BMW M1 Hood"><br/> <img class="lazy img-responsive" data-original="/img/bmw_m1_side.jpg" width="765" height="574" alt="BMW M1 Side"><br/> <img class="lazy img-responsive" data-original="/img/viper_1.jpg" width="765" height="574" alt="Viper 1"><br/> <img class="lazy img-responsive" data-original="/img/viper_corner.jpg" width="765" height="574" alt="Viper Corner"><br/> <img class="lazy img-responsive" data-original="/img/bmw_m3_gt.jpg" width="765" height="574" alt="BMW M3 GT"><br/> <img class="lazy img-responsive" data-original="/img/corvette_pitstop.jpg" width="765" height="574" alt="Corvette Pitstop"><br/> </p> <p>Aliquam erat volutpat. Maecenas non tortor nulla, non malesuada velit. Nullam felis tellus, tristique nec egestas in, luctus sed diam. Suspendisse potenti. </p> <p>Consectetur adipiscing elit. Nulla consectetur libero consectetur quam consequat nec tincidunt massa feugiat. Donec egestas mi turpis. Fusce adipiscing dui eu metus gravida vel facilisis ligula iaculis. Cras a rhoncus massa. Donec sed purus eget nunc placerat consequat.</p> <p>Cras venenatis condimentum nibh a mollis. Duis id sapien nibh. Vivamus porttitor, felis quis blandit tincidunt, erat magna scelerisque urna, a faucibus erat nisl eget nisl. Aliquam consequat turpis id velit egestas a posuere orci semper. Mauris suscipit erat quis urna adipiscing ultricies. In hac habitasse platea dictumst. Nulla scelerisque lorem quis dui sagittis egestas.</p> <p>Etiam sed massa felis, aliquam pellentesque est.</p> <p>the end.</p> </div>
To reduce the number of lazyload() during scrolling, we could use, for example, the mcs.top property for the top position of the scroll content (pixels):
function WhileScrolling() { // Debug: // console.log( 'top: ' + mcs.top ); // Run lazyload in 10 pixel steps (adjust to your needs) if( 0 == mcs.top % 10 ) { // Run lazyload on the "div#conent_1" box: $("#content_1 img.lazy").lazyload(); // Debug: //console.log( 'lazyload - mcs.top: ' + mcs.top ); } }
where we restrict the layzload selector, so we donβt need to find all the images in the entire DOM tree.
I noticed that in Internet Explorer 11 mcs.top can be floating numbers, but it also contains integers in Chrome.
So, we can install it using Math.floor() .
To further reduce lazyload calls, we could further compare mcs.top with its previous value:
var mcsTopPrev = 0; var mcsTop = 0; function WhileScrolling() {