当前位置:   article > 正文

jquery.lazyload.js源码欣赏

easylazyload.js源码

官方Github地址:https://github.com/tuupola/jquery_lazyload/

1.9.1版本 从.min.js中还原出 无注释

  1. (function($) {
  2. $.fn.lazyload = function(options) {
  3. var settings = {
  4. threshold: 0,
  5. failurelimit: 0,
  6. event: "scroll",
  7. effect: "show",
  8. container: window
  9. };
  10. if (options) {
  11. $.extend(settings, options)
  12. }
  13. var elements = this;
  14. if ("scroll" == settings.event) {
  15. $(settings.container).bind("scroll", function(event) {
  16. var counter = 0;
  17. elements.each(function() {
  18. if (!$.belowthefold(this, settings) && !$.rightoffold(this, settings)) {
  19. $(this).trigger("appear")
  20. } else {
  21. if (counter++ > settings.failurelimit) {
  22. return false
  23. }
  24. }
  25. });
  26. var temp = $.grep(elements, function(element) {
  27. return !element.loaded
  28. });
  29. elements = $(temp)
  30. })
  31. }
  32. return this.each(function() {
  33. var self = this;
  34. $(self).attr("original", $(self).attr("src"));
  35. if ("scroll" != settings.event || $.belowthefold(self, settings) || $.rightoffold(self, settings)) {
  36. if (settings.placeholder) {
  37. $(self).attr("src", settings.placeholder)
  38. } else {
  39. $(self).removeAttr("src")
  40. }
  41. self.loaded = false
  42. } else {
  43. self.loaded = true
  44. }
  45. $(self).one("appear", function() {
  46. if (!this.loaded) {
  47. $("<img />").bind("load", function() {
  48. $(self).hide().attr("src", $(self).attr("original"))[settings.effect](settings.effectspeed);
  49. self.loaded = true
  50. }).attr("src", $(self).attr("original"))
  51. }
  52. });
  53. if ("scroll" != settings.event) {
  54. $(self).bind(settings.event, function(event) {
  55. if (!self.loaded) {
  56. $(self).trigger("appear")
  57. }
  58. })
  59. }
  60. })
  61. };
  62. $.belowthefold = function(element, settings) {
  63. if (settings.container === undefined || settings.container === window) {
  64. var fold = $(window).height() + $(window).scrollTop()
  65. } else {
  66. var fold = $(settings.container).offset().top + $(settings.container).height()
  67. }
  68. return fold <= $(element).offset().top - settings.threshold
  69. };
  70. $.rightoffold = function(element, settings) {
  71. if (settings.container === undefined || settings.container === window) {
  72. var fold = $(window).width() + $(window).scrollLeft()
  73. } else {
  74. var fold = $(settings.container).offset().left + $(settings.container).width()
  75. }
  76. return fold <= $(element).offset().left - settings.threshold
  77. };
  78. $.extend($.expr[':'], {
  79. "below-the-fold": "$.belowthefold(a, {threshold : 0, container: window})",
  80. "above-the-fold": "!$.belowthefold(a, {threshold : 0, container: window})",
  81. "right-of-fold": "$.rightoffold(a, {threshold : 0, container: window})",
  82. "left-of-fold": "!$.rightoffold(a, {threshold : 0, container: window})"
  83. })
  84. })(jQuery);

1.9.3最新版 有注释

  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.3
  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 : true,
  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);


版权声明:本文为博主原创文章,未经博主允许不得转载。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/120899
推荐阅读
相关标签
  

闽ICP备14008679号