jquery.lazyload.js 8.9 KB

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