var Lazypager; Lazypager = (function() { function Lazypager(options) { this.options = { debug: true, container: "#catalog", navSelector: "a.infinite-loader", nextSelector: "a.infinite-loader:last", blockSelector: ".item", img: "", imgClass: 'infinite-loader-img', callback: function(elements) {} }; $.extend(this.options, options); this.setObjects(); } Lazypager.prototype.setObjects = function() { var self; this.url = $(this.options.nextSelector).attr('href'); this.container = $(this.options.container); this.paginator = $(this.options.navSelector); this.image = $(this.options.img).addClass(this.options.imgClass); if (this.paginator.size() < 1) { return; } this.visible = false; self = this; return $(window).scroll(function() { if (self.paginatorVisible()) { if (!self.visible) { return self.visibleHandler(); } } }); }; Lazypager.prototype.visibleHandler = function() { this.visible = true; this.paginator.replaceWith(this.image); return this.loadElements(); }; Lazypager.prototype.paginatorVisible = function() { var docViewBottom, docViewTop, elemBottom, elemTop; if (this.paginator.size() < 1) { return false; } docViewTop = $(window).scrollTop(); docViewBottom = docViewTop + $(window).height(); elemTop = this.paginator.offset().top; elemBottom = elemTop + this.paginator.height(); return (elemBottom <= docViewBottom) && (elemTop >= docViewTop); }; Lazypager.prototype.loadElements = function() { var self; self = this; return $.ajax({ dataType: "text", url: self.url }).done(function(data) { var elements, paginator; $("img." + self.options.imgClass).remove(); elements = $(data).find(self.options.blockSelector); paginator = $(data).find(self.options.navSelector); self.options.callback(elements); self.container.append(elements); self.container.parent().append(paginator); return self.setObjects(); }); }; return Lazypager; })(); window.Lazypager || (window.Lazypager = Lazypager);