5 lines
3.0 KiB
JavaScript
5 lines
3.0 KiB
JavaScript
![]() |
/*!
|
||
|
* 懒加载 - 因兼容问题将 data-lazy-src 替换为data-image
|
||
|
* https://github.com/mqyqingfeng/LazyLoad
|
||
|
*/
|
||
|
!function(){function t(t){this.opts=n.extend({},this.constructor.defaultOpts,t),this.init()}var e="object"==typeof self&&self.self==self&&self||"object"==typeof global&&global.global==global&&global||this||{};Function.prototype.bind=Function.prototype.bind||function(t){if("function"!=typeof this)throw new Error("Function.prototype.bind - what is trying to be bound is not callable");var e=this,n=Array.prototype.slice.call(arguments,1),o=function(){},i=function(){var i=Array.prototype.slice.call(arguments);e.apply(this instanceof o?this:t,n.concat(i))};return o.prototype=this.prototype,i.prototype=new o,i};var n={extend:function(t){for(var e=1,n=arguments.length;e<n;e++)for(var o in arguments[e])arguments[e].hasOwnProperty(o)&&(t[o]=arguments[e][o]);return t},addEvent:function(t,e,n){if(document.addEventListener)return t.addEventListener(e,n,!1),n;if(document.attachEvent){var o=function(){return n.apply(t,arguments)};return t.attachEvent("on"+e,o),o}},removeEvent:function(t,e,n){document.removeEventListener?t.removeEventListener(e,n,!1):t.detachEvent("on"+e,n)}};t.VERSION="1.0.0",t.defaultOpts={delay:250,useDebounce:!1};var o=t.prototype;o.init=function(){this.calulateView(),this.bindScrollEvent()},o.calulateView=function(){this.view={top:0-(parseInt(this.opts.top,10)||0),bottom:(e.innerHeight||document.documentElement.clientHeight)+(parseInt(this.opts.bottom,10)||0),left:0-(parseInt(this.opts.left,10)||0),right:(e.innerWidth||document.documentElement.clientWidth)+(parseInt(this.opts.right,10)||0)}},o.bindScrollEvent=function(){var t=n.addEvent(e,"scroll",this.handleLazyLoad.bind(this)),o=n.addEvent(e,"load",this.handleLazyLoad.bind(this));this.event={scrollEvent:t,loadEvent:o}};var i=null;o.handleLazyLoad=function(){var t=this;!this.opts.useDebounce&&i||(clearTimeout(i),i=setTimeout(function(){i=null,t.render()},this.opts.delay))},o.isHidden=function(t){return null===t.offsetParent},o.checkInView=function(t){if(this.isHidden(t))return!1;var e=t.getBoundingClientRect();return e.right>=this.view.left&&e.bottom>=this.view.top&&e.left<=this.view.right&&e.top<=this.view.bottom},o.render=function(){for(var t=document.querySelectorAll("[data-image], [data-lazy-background]"),e=t.length,n=0;n<e;n++)elem=t[n],this.checkInView(elem)&&(null!==elem.getAttribute("data-lazy-background")?elem.style.backgroundImage="url("+elem.getAttribute("data-lazy-background")+")":elem.src!==(src=elem.getAttribute("data-image"))&&(elem.src=src),elem.removeAttribute("data-image"),elem.removeAttribute("data-lazy-background"),this.opts.onload&&"function"==typeof this.opts.onload&&this.opts.onload(elem));e||this.unbindScrollEvent()},o.unbindScrollEvent=function(){n.removeEvent(e,"scroll",this.event.scrollEvent),n.removeEvent(e,"load",this.event.loadEvent)},"undefined"==typeof exports||exports.nodeType?e.Lazy=t:("undefined"!=typeof module&&!module.nodeType&&module.exports&&(exports=module.exports=t),exports.Lazy=t)}();
|