我认为作者的意思是另外挂钩whileScrolling
这样的事件:
(function($){
$(window).load(function(){
$("#content_1").mCustomScrollbar({
scrollButtons:{
enable:true
},
callbacks:{
whileScrolling:function(){ WhileScrolling(); }
}
});
function WhileScrolling(){
$("img.lazy").lazyload();
}
});
})(jQuery);
HTML 容器如下所示:
<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>
为了减少lazyload()
滚动期间的数量,我们可以使用例如mcs.top
滚动内容顶部位置(像素)的属性:
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 );
}
}
我们限制了layzload选择器,因此我们不必在整个DOM树中查找所有图像。
我注意到在Internet Explorer 11中,mcs.top
可以是浮点数,但在Chrome中总是整数。
所以我们可以用Math.floor()
.
为了进一步减少延迟加载调用,我们还可以与mcs.top
之前的值进行比较:
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;
}