jquery.lazyload.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Lazy Load - jQuery plugin for lazy loading images
  3. *
  4. * Copyright (c) 2007-2013 Mika Tuupola
  5. *
  6. * Licensed under the MIT license:
  7. * http://www.opensource.org/licenses/mit-license.php
  8. *
  9. * Project home:
  10. * http://www.appelsiini.net/projects/lazyload
  11. *
  12. * Version: 1.8.4
  13. *
  14. */
  15. (function($, window, document, undefined) {
  16. var $window = $(window);
  17. $.fn.lazyload = function(options) {
  18. var elements = this;
  19. var $container;
  20. var settings = {
  21. threshold : 0,
  22. failure_limit : 0,
  23. event : "scroll",
  24. effect : "show",
  25. container : window,
  26. data_attribute : "original",
  27. skip_invisible : true,
  28. appear : null,
  29. load : null
  30. };
  31. function update() {
  32. var counter = 0;
  33. elements.each(function() {
  34. var $this = $(this);
  35. if (settings.skip_invisible && !$this.is(":visible")) {
  36. return;
  37. }
  38. if ($.abovethetop(this, settings) ||
  39. $.leftofbegin(this, settings)) {
  40. /* Nothing. */
  41. } else if (!$.belowthefold(this, settings) &&
  42. !$.rightoffold(this, settings)) {
  43. $this.trigger("appear");
  44. /* if we found an image we'll load, reset the counter */
  45. counter = 0;
  46. } else {
  47. if (++counter > settings.failure_limit) {
  48. return false;
  49. }
  50. }
  51. });
  52. }
  53. if(options) {
  54. /* Maintain BC for a couple of versions. */
  55. if (undefined !== options.failurelimit) {
  56. options.failure_limit = options.failurelimit;
  57. delete options.failurelimit;
  58. }
  59. if (undefined !== options.effectspeed) {
  60. options.effect_speed = options.effectspeed;
  61. delete options.effectspeed;
  62. }
  63. $.extend(settings, options);
  64. }
  65. /* Cache container as jQuery as object. */
  66. $container = (settings.container === undefined ||
  67. settings.container === window) ? $window : $(settings.container);
  68. /* Fire one scroll event per scroll. Not one scroll event per image. */
  69. if (0 === settings.event.indexOf("scroll")) {
  70. $container.bind(settings.event, function(event) {
  71. return update();
  72. });
  73. }
  74. this.each(function() {
  75. var self = this;
  76. var $self = $(self);
  77. self.loaded = false;
  78. /* When appear is triggered load original image. */
  79. $self.one("appear", function() {
  80. if (!this.loaded) {
  81. if (settings.appear) {
  82. var elements_left = elements.length;
  83. settings.appear.call(self, elements_left, settings);
  84. }
  85. $("<img />")
  86. .bind("load", function() {
  87. $self
  88. .hide()
  89. .attr("src", $self.data(settings.data_attribute))
  90. [settings.effect](settings.effect_speed);
  91. self.loaded = true;
  92. /* Remove image from array so it is not looped next time. */
  93. var temp = $.grep(elements, function(element) {
  94. return !element.loaded;
  95. });
  96. elements = $(temp);
  97. if (settings.load) {
  98. var elements_left = elements.length;
  99. settings.load.call(self, elements_left, settings);
  100. }
  101. })
  102. .attr("src", $self.data(settings.data_attribute));
  103. }
  104. });
  105. /* When wanted event is triggered load original image */
  106. /* by triggering appear. */
  107. if (0 !== settings.event.indexOf("scroll")) {
  108. $self.bind(settings.event, function(event) {
  109. if (!self.loaded) {
  110. $self.trigger("appear");
  111. }
  112. });
  113. }
  114. });
  115. /* Check if something appears when window is resized. */
  116. $window.bind("resize", function(event) {
  117. update();
  118. });
  119. /* With IOS5 force loading images when navigating with back button. */
  120. /* Non optimal workaround. */
  121. if ((/iphone|ipod|ipad.*os 5/gi).test(navigator.appVersion)) {
  122. $window.bind("pageshow", function(event) {
  123. if (event.originalEvent.persisted) {
  124. elements.each(function() {
  125. $(this).trigger("appear");
  126. });
  127. }
  128. });
  129. }
  130. /* Force initial check if images should appear. */
  131. $(window).load(function() {
  132. update();
  133. });
  134. return this;
  135. };
  136. /* Convenience methods in jQuery namespace. */
  137. /* Use as $.belowthefold(element, {threshold : 100, container : window}) */
  138. $.belowthefold = function(element, settings) {
  139. var fold;
  140. if (settings.container === undefined || settings.container === window) {
  141. fold = $window.height() + $window.scrollTop();
  142. } else {
  143. fold = $(settings.container).offset().top + $(settings.container).height();
  144. }
  145. return fold <= $(element).offset().top - settings.threshold;
  146. };
  147. $.rightoffold = function(element, settings) {
  148. var fold;
  149. if (settings.container === undefined || settings.container === window) {
  150. fold = $window.width() + $window.scrollLeft();
  151. } else {
  152. fold = $(settings.container).offset().left + $(settings.container).width();
  153. }
  154. return fold <= $(element).offset().left - settings.threshold;
  155. };
  156. $.abovethetop = function(element, settings) {
  157. var fold;
  158. if (settings.container === undefined || settings.container === window) {
  159. fold = $window.scrollTop();
  160. } else {
  161. fold = $(settings.container).offset().top;
  162. }
  163. return fold >= $(element).offset().top + settings.threshold + $(element).height();
  164. };
  165. $.leftofbegin = function(element, settings) {
  166. var fold;
  167. if (settings.container === undefined || settings.container === window) {
  168. fold = $window.scrollLeft();
  169. } else {
  170. fold = $(settings.container).offset().left;
  171. }
  172. return fold >= $(element).offset().left + settings.threshold + $(element).width();
  173. };
  174. $.inviewport = function(element, settings) {
  175. return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&
  176. !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
  177. };
  178. /* Custom selectors for your convenience. */
  179. /* Use as $("img:below-the-fold").something() or */
  180. /* $("img").filter(":below-the-fold").something() which is faster */
  181. $.extend($.expr[':'], {
  182. "below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },
  183. "above-the-top" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
  184. "right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },
  185. "left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },
  186. "in-viewport" : function(a) { return $.inviewport(a, {threshold : 0}); },
  187. /* Maintain BC for couple of versions. */
  188. "above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
  189. "right-of-fold" : function(a) { return $.rightoffold(a, {threshold : 0}); },
  190. "left-of-fold" : function(a) { return !$.rightoffold(a, {threshold : 0}); }
  191. });
  192. })(jQuery, window, document);