jquery.lazyload.js 9.8 KB

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