lazyload.js 9.6 KB

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