X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/0c64954c4295bea6529f13fa027341ce50dfd4fa..816a4653c0086eeaf57ba148864bb06678d9234c:/project/static/js/jquery.lazyload.js diff --git a/project/static/js/jquery.lazyload.js b/project/static/js/jquery.lazyload.js new file mode 100644 index 00000000..25c94869 --- /dev/null +++ b/project/static/js/jquery.lazyload.js @@ -0,0 +1,43 @@ +(function($) { + jQuery.fn.lazyload = function(pattern, options) { + var settings = { + threshold: 0, + scrollThreshold: 300, + placeholder: 'loading...', + checkInterval: 2000 + }; + + if (options) { + $.extend(settings, options); + } + + var container = this; + container.data('lastScroll', -10000); + + function aboveViewport(container, element, threshold) { + return $(container).offset().top >= $(element).offset().top + $(element).height() + threshold; + } + + function belowViewport(container, element, threshold) { + return $(container).offset().top + $(container).height() + threshold <= $(element).offset().top; + } + + function checkScroll() { + if (Math.abs(container.scrollTop() - container.data('lastScroll')) > settings.scrollThreshold) { + container.data('lastScroll', container.scrollTop()); + + $(pattern, container).each(function() { + if (aboveViewport(container, this, settings.threshold) + || belowViewport(container, this, settings.threshold)) { + $(this).html(settings.placeholder); + } else { + $(this).html(''); + } + }) + } + setTimeout(checkScroll, settings.checkInterval); + } + + checkScroll(); + }; +})(jQuery);