Created
February 10, 2014 06:55
-
-
Save alanerzhao/8911505 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var scrollLoad = { | |
refreshTimer : null, | |
init: function () { | |
this.getInViewportList(); | |
this.scrollLoad(); | |
}, | |
scrollLoad : function () { | |
var self = this; | |
$(window).on('scroll', function () { | |
if (self.refreshTimer) { | |
clearTimeout(self.refreshTimer); | |
self.refreshTimer = null; | |
} | |
self.refreshTimer = setTimeout(function () { | |
self.getInViewportList(); | |
},1e3); | |
}); | |
}, | |
belowthefold : function (element) { | |
var fold = $(window).height() + $(window).scrollTop(); | |
return fold <= $(element).offset().top; | |
}, | |
abovethetop : function (element) { | |
var top = $(window).scrollTop(); | |
return top >= $(element).offset().top + $(element).height(); | |
}, | |
inViewport : function (element) { | |
return !this.belowthefold(element) && !this.abovethetop(element) | |
}, | |
getInViewportList : function () { | |
var list = $('#bookList li'), | |
ret = [], | |
self = this; | |
list.each(function (i) { | |
var li = list.eq(i); | |
console.log(li); | |
if (self.inViewport(li)) { | |
self.loadImg(li); | |
} | |
}); | |
}, | |
loadImg : function (li) { | |
if (li.find('img[_src]').length) { | |
var img = li.find('img[_src]'), | |
src = img.attr('_src'); | |
img.attr('src', src).hide().fadeIn(800).load(function () { | |
img.removeAttr('_src'); | |
}); | |
} | |
} | |
}; | |
$(function () { | |
scrollLoad.init(); | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment