forked from aoxls/lazyelem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazyelem.min.js
1 lines (1 loc) · 1.45 KB
/
lazyelem.min.js
1
!function(a,b){"use strict";var c,d,e=b(a),f=!1,g=[],h=[],i={timeout:10,buffer:100,loadingClass:"lazy-loading",srcValue:"lazy-src",bgValue:"lazy-bg"},j={_init:function(){c=this},listen:function(a,d,e){var j,k=a,l=d||"img";"object"!=(typeof a).toLowerCase()&&(k=b(a||"img["+i.srcValue+"]")),e&&(j=h.push(e)-1),k.each(function(){var a=b(this);g.push({type:l,obj:a,callback:e?h[j]:null}),"img"==l&&a.addClass(i.loadingClass)}),f||c._startListen(),c.detect()},_startListen:function(){e.bind("scroll.lazyelem resize.lazyelem",function(){g.length&&(clearTimeout(d),d=setTimeout(function(){c.detect()},i.timeout))}),f=!0},detect:function(){for(var a=0;a<g.length;a++){var b=g[a],d=b.obj;if(!d.is(":hidden")&&c._isTrigger(d)){switch(b.type){case"fn":break;case"img":var h=d.attr(i.srcValue);h&&d.attr("src",h).removeAttr(i.srcValue);break;case"bg":var j=d.attr(i.bgValue);j&&d.css("background-image","url("+j+")").removeAttr(i.bgValue);break;case"dom":var k=d.children("script");k.length&&k.replaceWith(c._minHtml(k.html()))}g.splice(a--,1),b.callback&&b.callback(d)}}0===g.length&&(e.unbind("scroll.lazyelem resize.lazyelem"),f=!1)},_isTrigger:function(a){var b=e.height(),c=e.scrollTop(),d=a.height(),f=a.offset().top;return f+d>c-i.buffer&&f<c+b+i.buffer},_minHtml:function(a){var b=/\n+/g,c=/<!--.*?-->/gi,d=/\/\*.*?\*\//gi,e=/[ ]+</gi;return a=a.replace(b,""),a=a.replace(c,""),a=a.replace(d,""),a=a.replace(e,"<")},config:function(a){b.extend(i,a)}};j._init(),a.lazyelem=j}(window,jQuery);