jquery.lazyload.js 8.9 KB

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