2 * Lazy Load - jQuery plugin for lazy loading images
4 * Copyright (c) 2007-2009 Mika Tuupola
6 * Licensed under the MIT license:
7 * http://www.opensource.org/licenses/mit-license.php
10 * http://www.appelsiini.net/projects/lazyload
17 $.fn.lazyload = function(options) {
27 $.extend(settings, options);
30 /* Fire one scroll event per scroll. Not one scroll event per image. */
32 if ("scroll" == settings.event) {
33 $(settings.container).bind("scroll", function(event) {
35 elements.each(function() {
36 if (!$.belowthefold(this, settings) &&
37 !$.rightoffold(this, settings)) {
38 $(this).trigger("appear");
40 if (counter++ > settings.failurelimit) {
45 /* Remove image from array so it is not looped next time. */
46 var temp = $.grep(elements, function(element) {
47 return !element.loaded;
53 return this.each(function() {
55 /* TODO: use .data() instead of .attr() */
56 $(self).attr("original", $(self).attr("src"));
57 if ("scroll" != settings.event
58 || $.belowthefold(self, settings)
59 || $.rightoffold(self, settings)) {
60 if (settings.placeholder) {
61 $(self).attr("src", settings.placeholder);
63 $(self).removeAttr("src");
70 /* When appear is triggered load original image. */
71 $(self).one("appear", function() {
74 .bind("load", function() {
77 .attr("src", $(self).attr("original"))
78 [settings.effect](settings.effectspeed);
81 .attr("src", $(self).attr("original"));
85 /* When wanted event is triggered load original image */
86 /* by triggering appear. */
87 if ("scroll" != settings.event) {
88 $(self).bind(settings.event, function(event) {
90 $(self).trigger("appear");
98 /* Convenience methods in jQuery namespace. */
99 /* Use as $.belowthefold(element, {threshold : 100, container : window}) */
101 $.belowthefold = function(element, settings) {
102 if (settings.container === undefined || settings.container === window) {
103 var fold = $(window).height() + $(window).scrollTop();
106 var fold = $(settings.container).offset().top + $(settings.container).height();
108 return fold <= $(element).offset().top - settings.threshold;
111 $.rightoffold = function(element, settings) {
112 if (settings.container === undefined || settings.container === window) {
113 var fold = $(window).width() + $(window).scrollLeft();
116 var fold = $(settings.container).offset().left + $(settings.container).width();
118 return fold <= $(element).offset().left - settings.threshold;
121 $.aboveViewport = function(element, settings) {
122 if (settings.container === undefined || settings.container === window) {
123 var top = $(window).scrollTop();
126 var top = $(settings.container).offset().top;
128 return top >= $(element).offset().bottom - settings.threshold;
131 /* Custom selectors for your convenience. */
132 /* Use as $("img:below-the-fold").something() */
134 $.extend($.expr[':'], {
135 "below-the-fold" : "$.belowthefold(a, {threshold : 0, container: window})",
136 "above-the-fold" : "!$.belowthefold(a, {threshold : 0, container: window})",
137 "right-of-fold" : "$.rightoffold(a, {threshold : 0, container: window})",
138 "left-of-fold" : "!$.rightoffold(a, {threshold : 0, container: window})"