//lazy load image(function ($) { $.fn.onScroll = function (options) { var defaultOpt = { callback: null, params: null, reserve: 0 }; var opt = $.extend({}, defaultOpt, options); var $this = $(this);
var _checkScreen = function(ignoreCache){ var scrolltop = $(window).scrollTop(); var wheight = $(window).height(); var offsetTop = $this.offset().top; if (offsetTop >= scrolltop && offsetTop <= (scrolltop + wheight + (ignoreCache === true ?0:opt.reserve))) { if ($.isFunction(opt.callback)) { opt.callback($this, opt.params); } else { $(document).trigger(opt.callback, [$this, opt.params]); } } } $(document).on('dom.scroll', _checkScreen); _checkScreen(true); };
$(document).on('dom.load', function () { $("[data-onscroll]").each(function () { var $this = $(this); $this.onScroll({ callback: $this.attr('data-onscroll'), params: $this.attr('data-onscroll-params'), reserve: Number($this.attr('data-reserve')) || 0 }) $this.removeAttr("data-onscroll"); }); });
})(jQuery);
PlugIn for lazy loading items.
Usage:
<iframe class="shown" data-onscroll="informaScrollToEvent" data-src="<%= informaUrl %>" width="100%" frameborder="0" height="500px" style="border:1px solid #d7d7d7;"></iframe>
$(document).one('informaScrollToEvent', function (e, iframe, params) {
var url = $(iframe).attr('data-src');
$(iframe).attr('src', url);
$(iframe).removeAttr('data-src');
});