X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/7f3f79476a57d10991566c511d40e20154c01064..78644811ca0c6042212788dc67add42bc41fb74c:/platforma/static/js/lib/jquery.lazyload.js?ds=sidebyside diff --git a/platforma/static/js/lib/jquery.lazyload.js b/platforma/static/js/lib/jquery.lazyload.js new file mode 100755 index 00000000..c15167bd --- /dev/null +++ b/platforma/static/js/lib/jquery.lazyload.js @@ -0,0 +1,52 @@ +(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('lazyload:lastCheckedScrollTop', -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 (container.data('lazyload:lastCheckedScrollTop') == undefined) { + return; + } + if (Math.abs(container.scrollTop() - container.data('lazyload:lastCheckedScrollTop')) > settings.scrollThreshold) { + container.data('lazyload:lastCheckedScrollTop', container.scrollTop()); + + $(pattern, container).each(function() { + if (aboveViewport(container, this, settings.threshold) + || belowViewport(container, this, settings.threshold)) { + $(this).html(settings.placeholder); + } else { + $(this).html(''); + var self = this; + $('').load(function() { + if ($(this).height() > $(self).height()) { + $(self).height($(this).height()); + } + }).appendTo(this); + } + }) + } + setTimeout(checkScroll, settings.checkInterval); + } + + checkScroll(); + }; +})(jQuery);