2 jQuery.fn.lazyload = function(pattern, options) {
6 placeholder: 'loading...',
11 $.extend(settings, options);
15 container.data('lastScroll', -10000);
17 function aboveViewport(container, element, threshold) {
18 return $(container).offset().top >= $(element).offset().top + $(element).height() + threshold;
21 function belowViewport(container, element, threshold) {
22 return $(container).offset().top + $(container).height() + threshold <= $(element).offset().top;
25 function checkScroll() {
26 if (Math.abs(container.scrollTop() - container.data('lastScroll')) > settings.scrollThreshold) {
27 container.data('lastScroll', container.scrollTop());
29 $(pattern, container).each(function() {
30 if (aboveViewport(container, this, settings.threshold)
31 || belowViewport(container, this, settings.threshold)) {
32 $(this).html(settings.placeholder);
36 $('<img src="' + $(this).attr('src') + '" width="' + $(this).width() + '" />').load(function() {
37 if ($(this).height() > $(self).height()) {
38 $(self).height($(this).height());
44 setTimeout(checkScroll, settings.checkInterval);