jquery.lazyload.js 8.8 KB

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