forked from dkern/jquery.lazy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.lazy.min.js
14 lines (14 loc) · 3.45 KB
/
jquery.lazy.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*!
* jQuery Lazy - min - v0.3.6
* http://jquery.eisbehr.de/lazy/
* http://eisbehr.de/
*
* Copyright 2014, Daniel 'Eisbehr' Kern
*
* Dual licensed under the MIT and GPL-2.0 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl-2.0.html
*
* jQuery("img.lazy").lazy();
*/
(function(e,t,n,r){e.fn.lazy=function(i){"use strict";function p(){if(s.delay>=0)setTimeout(function(){d(true)},s.delay);if(s.delay<0||s.combined){d(false);x(function(){e(s.appendScroll).bind("scroll",b(s.throttle,function(){x(function(){d(false)},this,true)}))},this);x(function(){e(s.appendScroll).bind("resize",b(s.throttle,function(){a=f=-1;x(function(){d(false)},this,true)}))},this)}}function d(t){if(!o.length)return;var n=false;for(var r=0;r<o.length;r++){(function(){var i=o[r],u=e(i);if(m(i)||t){var a=o[r].tagName.toLowerCase();if(u.attr(s.attribute)&&(a=="img"&&u.attr(s.attribute)!=u.attr("src")||a!="img"&&u.attr(s.attribute)!=u.css("background-image"))&&!u.data(s.handledName)&&(u.is(":visible")||!s.visibleOnly)){n=true;u.data(s.handledName,true);x(function(){v(u,a)},this)}}})()}if(n)x(function(){o=e(o).filter(function(){return!e(this).data(s.handledName)})},this)}function v(n,r){var i=e(new Image);++u;if(s.onError)i.error(function(){E(s.onError,n);w()});else i.error(function(){w()});var o=false;i.one("load",function(){var e=function(){if(!o){t.setTimeout(e,100);return}n.hide();if(r=="img")n.attr("src",i.attr("src"));else n.css("background-image","url("+i.attr("src")+")");n[s.effect](s.effectTime);if(s.removeAttribute)n.removeAttr(s.attribute);E(s.afterLoad,n);i.unbind("load").remove();w()};e()});E(s.beforeLoad,n);i.attr("src",n.attr(s.attribute));E(s.onLoad,n);o=true;if(i.complete)i.load()}function m(e){var t=g(),n=y(),r=e.getBoundingClientRect(),i=n+s.threshold>r.top&&-s.threshold<r.bottom,o=t+s.threshold>r.left&&-s.threshold<r.right;if(s.scrollDirection=="vertical")return i;else if(s.scrollDirection=="horizontal")return o;return i&&o}function g(){if(a>=0)return a;a=t.innerWidth||n.documentElement.clientWidth||n.body.clientWidth||n.body.offsetWidth||s.fallbackWidth;return a}function y(){if(f>=0)return f;f=t.innerHeight||n.documentElement.clientHeight||n.body.clientHeight||n.body.offsetHeight||s.fallbackHeight;return f}function b(e,t){function o(){function u(){i=+(new Date);t.apply(r)}var o=+(new Date)-i;n&&clearTimeout(n);if(o>e||!s.enableThrottle)u();else n=setTimeout(u,e-o)}var n,i=0;return o}function w(){--u;if(!o.size()&&!u)E(s.onFinishedAll,null)}function E(e,t){if(e){if(t)x(function(){e(t)},this);else x(e,this)}}function S(){l=setTimeout(function(){x();if(c.length)S()},2)}function x(e,n,r){if(e){if(s.enableQueueing)e.call(n||t);if(!r||r&&!h){c.push([e,n,r]);if(r)h=true}if(c.length==1)S();return 0}var i=c.shift();if(!i)return 0;if(i[2])h=false;i[0].call(i[1]||t);return 0}var s={bind:"load",threshold:500,fallbackWidth:2e3,fallbackHeight:2e3,visibleOnly:false,appendScroll:t,scrollDirection:"both",delay:-1,combined:false,attribute:"data-src",removeAttribute:true,handledName:"handled",effect:"show",effectTime:0,enableThrottle:false,throttle:250,enableQueueing:true,beforeLoad:null,onLoad:null,afterLoad:null,onError:null,onFinishedAll:null},o=this,u=0,a=-1,f=-1,l=null,c=[],h=false;(function(){if(i)e.extend(s,i);if(s.onError)o.each(function(){var t=this;x(function(){e(t).bind("error",function(){E(s.onError,e(this))})},t)});if(s.bind=="load")e(t).load(p);else if(s.bind=="event")p()})();return this};e.fn.Lazy=e.fn.lazy})(jQuery,window,document);