我正在写我自己的图像懒加载函数,当div滚动到其底部,我们加载一些新的图像,容器div的高度(#ScrollDiv
在这个案件)增加,当我们再次滚动到底部时,我们进行相同的调用。虽然我在每次请求更多图像(这称为appName.featureName.PaginationConstant
和父范围)中传递“分页ID”,但我想移除或冻结滚动事件,因此我们不会提出其他请求或增加分页ID。例如:删除并添加滚动事件处理程序使用jQuery .off或.unbind
appName.featureName.lazyLoader = function() {
var currentScroll = $(this).scrollTop(),
divHeight = $(this)[0].scrollHeight,
actualHeight = $(this).height() + parseInt($(this).css('padding-bottom'))
// have we hit the bottom of the Scroll Div?
if((divHeight - actualHeight) <= currentScroll) {
// yes we have, remove the scroll, see I name this function below
$('#ScrollDiv').off('scroll', appName.featureName.lazyLoader);
// Now get more photos, in the function below I shall re-bind the scroll functionality
appName.featureName.getMorePhotos(++appName.featureName.PaginationConstant);
}
};
// this is the lazyload funtion
appName.featureName.lazyLoad = function() {
$('#ScrollDiv').on('scroll', appName.featureName.lazyLoader);
};
除了解绑,一切都很好用!我仍然能够触发滚动事件处理程序,尽管事实上我试图删除它,一旦我的条件遇到与$('#ScrollDiv').off('scroll', appName.featureName.lazyLoader);
我做错了什么?
你可能会更好地切换一个布尔值,该值指示是否应该运行该函数,而不是不断地绑定和解除绑定事件处理程序。 “appName.featureName.getMorePhotos”函数的代码是什么样的? – 2014-11-06 14:03:28
我认为你是正确的布尔比绑定和解除绑定...即使在下面的答案我无法得到的东西工作,当我通过布尔一切正常! – 2014-11-06 14:16:15