2 jQuery.fn.lazyload = function(pattern, options) {
6 placeholder: 'loading...',
11 $.extend(settings, options);
15 container.data('lazyload:lastCheckedScrollTop', -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 (container.data('lazyload:lastCheckedScrollTop') == undefined) {
29 if (Math.abs(container.scrollTop() - container.data('lazyload:lastCheckedScrollTop')) > settings.scrollThreshold) {
30 container.data('lazyload:lastCheckedScrollTop', container.scrollTop());
32 $(pattern, container).each(function() {
33 if (aboveViewport(container, this, settings.threshold)
34 || belowViewport(container, this, settings.threshold)) {
35 $(this).html(settings.placeholder);
39 $('<img src="' + $(this).attr('src') + '" width="' + $(this).width() + '" />').load(function() {
40 if ($(this).height() > $(self).height()) {
41 $(self).height($(this).height());
47 setTimeout(checkScroll, settings.checkInterval);