(function($) { $.fn.lazyload = function(options) { var settings = { threshold : 0 }; if(options) { $.extend(settings, options); }; var elements = this; if (!settings.event) { $(window).bind("scroll", function(event) { elements.each(function() { if (!this.loaded && !$.belowthefold(this, settings) && !$.rightoffold(this, settings)) { $(this).attr("src", $(this).attr("original")); this.loaded = true; }; }); var temp = $.grep(elements, function(element) { return !element.loaded; }); elements = $(temp); }); }; return this.each(function() { var self = this; $(self).attr("original", $(self).attr("src")); if (settings.event || $.belowthefold(self, settings) || $.rightoffold(self, settings)) { if (settings.placeholder) { $(self).attr("src", settings.placeholder); } else { $(self).removeAttr("src"); } self.loaded = false; } else { self.loaded = true; } if (settings.event) { $(self)[settings.event](function(event) { if (!self.loaded) { $(self).attr("src", $(self).attr("original")); self.loaded = true; }; }); }; }); }; $.belowthefold = function(element, settings) { var fold = $(window).height() + $(window).scrollTop(); return fold <= $(element).offset().top - settings.threshold; }; $.rightoffold = function(element, settings) { var fold = $(window).width() + $(window).scrollLeft(); return fold <= $(element).offset().left - settings.threshold; }; $.extend($.expr[':'], { belowthefold : "$.belowthefold(a, {threshold : 0})", abovethefold : "!$.belowthefold(a, {threshold : 0})", rightoffold : "$.rightoffold(a, {threshold : 0})", leftoffold : "!$.rightoffold(a, {threshold : 0})" }); })(jQuery);