I think the author meant additionally hooking into the whileScrolling
event like this:
(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 like the following:
<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 scrolling content’s top position (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 have to find all the images in the whole DOM tree.
I noticed that in Internet Explorer 11, the mcs.top
can be floating numbers but it's alwyas whole integers in Chrome.
So we could floor it with Math.floor()
.
To further reduce the lazyload calls, we could additionally compare mcs.top
to it's previous value:
var mcsTopPrev = 0;
var mcsTop = 0;
function WhileScrolling()
{
// Fix the Chrome vs Internet Explorer difference
mcsTop = Math.floor( mcs.top );
// Current vs previous
if( mcsTop != mcsTopPrev )
{
// Run lazyload in 10px scrolling steps (adjust to your needs)
if( 0 == mcsTop % 10 )
{
$("#cphContent_lReferences img.lazy").lazyload();
}
}
mcsTopPrev = mcsTop;
}