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
18
$.fn.lazyload = function(options) {
28
$.extend(settings, options);
31
/* Fire one scroll event per scroll. Not one scroll event per image. */
33
if ("scroll" == settings.event) {
34
$(settings.container).bind("scroll", function(event) {
37
elements.each(function() {
38
if ($.abovethetop(this, settings) ||
39
$.leftofbegin(this, settings)) {
41
} else if (!$.belowthefold(this, settings) &&
42
!$.rightoffold(this, settings)) {
43
$(this).trigger("appear");
45
if (counter++ > settings.failurelimit) {
50
/* Remove image from array so it is not looped next time. */
51
var temp = $.grep(elements, function(element) {
52
return !element.loaded;
58
this.each(function() {
61
/* Save original only if it is not defined in HTML. */
62
if (undefined == $(self).attr("original")) {
63
$(self).attr("original", $(self).attr("src"));
66
if ("scroll" != settings.event ||
67
undefined == $(self).attr("src") ||
68
settings.placeholder == $(self).attr("src") ||
69
($.abovethetop(self, settings) ||
70
$.leftofbegin(self, settings) ||
71
$.belowthefold(self, settings) ||
72
$.rightoffold(self, settings) )) {
74
if (settings.placeholder) {
75
$(self).attr("src", settings.placeholder);
77
$(self).removeAttr("src");
84
/* When appear is triggered load original image. */
85
$(self).one("appear", function() {
88
.bind("load", function() {
91
.attr("src", $(self).attr("original"))
92
[settings.effect](settings.effectspeed);
95
.attr("src", $(self).attr("original"));
99
/* When wanted event is triggered load original image */
100
/* by triggering appear. */
101
if ("scroll" != settings.event) {
102
$(self).bind(settings.event, function(event) {
104
$(self).trigger("appear");
110
/* Force initial check if images should appear. */
111
$(settings.container).trigger(settings.event);
117
/* Convenience methods in jQuery namespace. */
118
/* Use as $.belowthefold(element, {threshold : 100, container : window}) */
120
$.belowthefold = function(element, settings) {
121
if (settings.container === undefined || settings.container === window) {
122
var fold = $(window).height() + $(window).scrollTop();
124
var fold = $(settings.container).offset().top + $(settings.container).height();
126
return fold <= $(element).offset().top - settings.threshold;
129
$.rightoffold = function(element, settings) {
130
if (settings.container === undefined || settings.container === window) {
131
var fold = $(window).width() + $(window).scrollLeft();
133
var fold = $(settings.container).offset().left + $(settings.container).width();
135
return fold <= $(element).offset().left - settings.threshold;
138
$.abovethetop = function(element, settings) {
139
if (settings.container === undefined || settings.container === window) {
140
var fold = $(window).scrollTop();
142
var fold = $(settings.container).offset().top;
144
return fold >= $(element).offset().top + settings.threshold + $(element).height();
147
$.leftofbegin = function(element, settings) {
148
if (settings.container === undefined || settings.container === window) {
149
var fold = $(window).scrollLeft();
151
var fold = $(settings.container).offset().left;
153
return fold >= $(element).offset().left + settings.threshold + $(element).width();
155
/* Custom selectors for your convenience. */
156
/* Use as $("img:below-the-fold").something() */
158
$.extend($.expr[':'], {
159
"below-the-fold" : "$.belowthefold(a, {threshold : 0, container: window})",
160
"above-the-fold" : "!$.belowthefold(a, {threshold : 0, container: window})",
161
"right-of-fold" : "$.rightoffold(a, {threshold : 0, container: window})",
162
"left-of-fold" : "!$.rightoffold(a, {threshold : 0, container: window})"
b'\\ No newline at end of file'