PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/ajax/libs/jquery.lazy/0.1.10/jquery.lazy.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 276 lines | 135 code | 44 blank | 97 comment | 46 complexity | 2538afb99d7602177e65103d6d21b4e3 MD5 | raw file
  1. /*!
  2. * jQuery Lazy - v0.1.10
  3. * http://jquery.eisbehr.de/lazy/
  4. *
  5. * Copyright 2013, Daniel 'Eisbehr' Kern
  6. *
  7. * Dual licensed under the MIT and GPL v2 licenses:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. * http://www.gnu.org/licenses/gpl-2.0.html
  10. *
  11. * jQuery("img.lazy").lazy();
  12. */
  13. (function($, window, document, undefined)
  14. {
  15. $.fn.lazy = function(settings)
  16. {
  17. /**
  18. * settings and configuration data
  19. * @type {}
  20. */
  21. var configuration =
  22. {
  23. // general
  24. bind : "load",
  25. threshold : 500,
  26. fallbackHeight : 2000,
  27. visibleOnly : true,
  28. // delay
  29. delay : -1,
  30. combined : false,
  31. // attribute
  32. attribute : "data-src",
  33. removeAttribute : true,
  34. // effect
  35. effect : "show",
  36. effectTime : 0,
  37. // throttle
  38. enableThrottle : false,
  39. throttle : 250,
  40. // callback
  41. beforeLoad : null,
  42. onLoad : null,
  43. afterLoad : null,
  44. onError : null
  45. };
  46. // overwrite configuration with custom user settings
  47. if( settings ) $.extend(configuration, settings);
  48. // all given items by jQuery selector
  49. var items = this;
  50. // on first page load get initial images
  51. if( configuration.bind == "load" ) $(window).load(_init);
  52. // if event driven don't wait for page loading
  53. else if( configuration.bind == "event" ) _init();
  54. // bind error callback to images if wanted
  55. if( configuration.onError ) items.bind("error", function() { configuration.onError($(this)); });
  56. /**
  57. * lazyLoadImages(allImages)
  58. *
  59. * the 'lazy magic'
  60. * check and load all needed images
  61. *
  62. * @param allImages boolean
  63. * @return void
  64. */
  65. function lazyLoadImages(allImages)
  66. {
  67. if( typeof allImages != "boolean" ) allImages = false;
  68. items.each(function()
  69. {
  70. var element = $(this);
  71. var tag = element.prop("tagName").toLowerCase();
  72. if( _isInLoadableArea(element) || allImages )
  73. {
  74. // the lazy magic
  75. if( // image source attribute is available
  76. element.attr(configuration.attribute) &&
  77. // and is image tag where attribute is not equal source
  78. ((tag == "img" && element.attr(configuration.attribute) != element.attr("src")) ||
  79. // or is non image tag where attribute is not equal background
  80. ((tag != "img" && element.attr(configuration.attribute) != element.css("background-image"))) ) &&
  81. // and is not actually loaded just before
  82. !element.data("loaded") &&
  83. // and is visible or visibility doesn't matter
  84. (element.is(":visible") || !configuration.visibleOnly) )
  85. {
  86. // create image object
  87. var imageObj = $(new Image());
  88. // copy element information to pseudo image beacuse we return this element in "onLoad" and "onError"
  89. $.each(element.prop("attributes"), function(index, attr)
  90. {
  91. if( attr.name != "src" )
  92. {
  93. // i know, there is a shorter way to do the following
  94. // but this is the beste worktaround for ie6/7
  95. var value = element.attr(attr.name);
  96. imageObj.attr(attr.name, value);
  97. }
  98. });
  99. // bind error event if wanted
  100. if( configuration.onError ) imageObj.error(function() { configuration.onError(imageObj); });
  101. // bind after load callback to image
  102. var onLoad = true;
  103. imageObj.one("load", function()
  104. {
  105. var callable = function()
  106. {
  107. if( onLoad )
  108. {
  109. window.setTimeout(callable, 100);
  110. return;
  111. }
  112. // remove element from view
  113. element.hide();
  114. // set image back to element
  115. if( tag == "img" ) element.attr("src", imageObj.attr("src"));
  116. else element.css("background-image", "url(" + imageObj.attr("src") + ")");
  117. // bring it back with some effect!
  118. element[configuration.effect](configuration.effectTime);
  119. // remove attribute from element
  120. if( configuration.removeAttribute ) element.removeAttr(configuration.attribute);
  121. // call after load event
  122. if( configuration.afterLoad ) configuration.afterLoad(element);
  123. // unbind event and remove image object
  124. imageObj.unbind("load");
  125. imageObj.remove();
  126. };
  127. callable();
  128. });
  129. // trigger function before loading image
  130. if( configuration.beforeLoad ) configuration.beforeLoad(element);
  131. // set source
  132. imageObj.attr("src", element.attr(configuration.attribute));
  133. // trigger function before loading image
  134. if( configuration.onLoad ) configuration.onLoad(imageObj);
  135. onLoad = false;
  136. // call after load even on cached image
  137. if( imageObj.complete ) imageObj.load();
  138. // mark element always as loaded
  139. element.data("loaded", true);
  140. }
  141. }
  142. });
  143. // cleanup all items which are allready loaded
  144. items = $(items).filter(function()
  145. {
  146. return !$(this).data("loaded");
  147. });
  148. }
  149. /**
  150. * _init()
  151. *
  152. * initialize lazy plugin
  153. * bind loading to events or set delay time to load all images at once
  154. *
  155. * @return void
  156. */
  157. function _init()
  158. {
  159. // if delay time is set load all images at once after delay time
  160. if( configuration.delay >= 0 ) setTimeout(function() { lazyLoadImages(true); }, configuration.delay);
  161. // if no delay is set or combine usage is active bind events
  162. if( configuration.delay < 0 || configuration.combined )
  163. {
  164. // load initial images
  165. lazyLoadImages(false);
  166. // bind lazy load functions to scroll and resize event
  167. $(window).bind("scroll", _throttle(configuration.throttle, lazyLoadImages));
  168. $(window).bind("resize", _throttle(configuration.throttle, lazyLoadImages));
  169. }
  170. }
  171. /**
  172. * _isInLoadableArea(element)
  173. *
  174. * check if the given element is inside the current viewport or threshold
  175. *
  176. * @param element jQuery
  177. * @return boolean
  178. */
  179. function _isInLoadableArea(element)
  180. {
  181. var top = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
  182. return (top + _getActualHeight() + configuration.threshold) > (element.offset().top + element.height());
  183. }
  184. /**
  185. * _getActualHeight()
  186. *
  187. * try to allocate current viewport height of the browser
  188. * uses fallback option when no height is found
  189. *
  190. * @return number
  191. */
  192. function _getActualHeight()
  193. {
  194. if( window.innerHeight ) return window.innerHeight;
  195. if( document.documentElement && document.documentElement.clientHeight ) return document.documentElement.clientHeight;
  196. if( document.body && document.body.clientHeight ) return document.body.clientHeight;
  197. if( document.body && document.body.offsetHeight ) return document.body.offsetHeight;
  198. return configuration.fallbackHeight;
  199. }
  200. /**
  201. * _throttle(delay, call)
  202. *
  203. * helper function to throttle down event triggering
  204. *
  205. * @param delay integer
  206. * @param call function object
  207. * @return function object
  208. */
  209. function _throttle(delay, call)
  210. {
  211. var _timeout;
  212. var _exec = 0;
  213. function callable()
  214. {
  215. var elapsed = +new Date() - _exec;
  216. function run()
  217. {
  218. _exec = +new Date();
  219. call.apply();
  220. }
  221. _timeout && clearTimeout(_timeout);
  222. if( elapsed > delay || !configuration.enableThrottle ) run();
  223. else _timeout = setTimeout(run, delay - elapsed);
  224. }
  225. return callable;
  226. }
  227. return this;
  228. };
  229. // make lazy a bit more caseinsensitive :)
  230. $.fn.Lazy = $.fn.lazy;
  231. }
  232. )(jQuery, window, document);