diff --git a/dist/juejin-lazyload.min.js b/dist/juejin-lazyload.min.js index 8121c9c..678d78e 100644 --- a/dist/juejin-lazyload.min.js +++ b/dist/juejin-lazyload.min.js @@ -1,6 +1,6 @@ /** - * juejin-lazyload v0.1.3 + * juejin-lazyload v0.1.4 * (c) 2017 WEBuster * @license MIT */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.JuejinLazyload=t()}(this,function(){"use strict";function e(e,t,n){var i=arguments.length>3&&void 0!==arguments[3]&&arguments[3];return e.addEventListener(t,n,i),function(){e.removeEventListener(t,n,i)}}!function(){function e(e){this.value=e}function t(t){function n(o,r){try{var a=t[o](r),s=a.value;s instanceof e?Promise.resolve(s.value).then(function(e){n("next",e)},function(e){n("throw",e)}):i(a.done?"return":"normal",a.value)}catch(e){i("throw",e)}}function i(e,t){switch(e){case"return":o.resolve({value:t,done:!0});break;case"throw":o.reject(t);break;default:o.resolve({value:t,done:!1})}(o=o.next)?n(o.key,o.arg):r=null}var o,r;this._invoke=function(e,t){return new Promise(function(i,a){var s={key:e,arg:t,resolve:i,reject:a,next:null};r?r=r.next=s:(o=r=s,n(e,t))})},"function"!=typeof t.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)}}();var t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},n=function(){function e(e,t){for(var n=0;n=t&&(n=Date.now(),setTimeout(function(){e.apply(null,o)},t))}}(function(){return t.updateState()},this.options.interval);this.removeScrollEventListener=e(window,"scroll",n),this.removeResizeEventListener=e(window,"resize",n)}}},{key:"removeEventListener",value:function(){this.removeScrollEventListener&&this.removeScrollEventListener(),this.removeResizeEventListener&&this.removeResizeEventListener()}},{key:"addOrUpdateElement",value:function(e){var t=this.getElementList(e),n=t.filter(function(e){return!e.__JUEJIN_LAZYLOAD});this.elementList=(this.elementList||[]).concat(n),t.forEach(this.initElement.bind(this)),this.updateState()}},{key:"removeElement",value:function(e){var t=this.getElementList(e);this.elementList=this.elementList.filter(function(e){return-1===t.indexOf(e)}),t.forEach(this.removeInfo.bind(this))}},{key:"clean",value:function(){this.elementList.forEach(this.removeInfo.bind(this)),this.elementList=[]}},{key:"getElementList",value:function(e){return e?"string"==typeof e?[].slice.call(document.querySelectorAll(e)):"number"==typeof e.length?[].slice.call(e):[e]:[]}},{key:"initElement",value:function(e){var t=this.options.infoGetter&&this.options.infoGetter(e),n=i({},t,{isImg:"IMG"===e.nodeName,loading:!1});n.hasPlaceholder=n.isImg&&n.width&&n.height,n.hasPlaceholder&&(e.src=function(e,t){return["data:image/svg+xml;utf8,",'','"].join("")}(n.width,n.height)),e.__JUEJIN_LAZYLOAD=n,this.updateElementClassByState("inited",e)}},{key:"removeInfo",value:function(e){e.__JUEJIN_LAZYLOAD&&(e.__JUEJIN_LAZYLOAD=null)}},{key:"updateState",value:function(){var e=this;if(this.elementList.length){var t=this.getActiveArea();this.elementList.forEach(function(n){var i=n.__JUEJIN_LAZYLOAD.loading,o=n.getBoundingClientRect(),r=function(e,t){return!(t.bottome.bottom||t.righte.right)}(t,o);!i&&r&&e.loadIamge(n)})}}},{key:"getActiveArea",value:function(){var e=this.getVisibleArea(),t=this.options.threshold||0;return{top:e.top-t,left:e.left-t,right:e.right+t,bottom:e.bottom+t}}},{key:"getVisibleArea",value:function(){if(this.options.visibleAreaGetter)return this.options.visibleAreaGetter();var e=function(){var e=document.documentElement;return{width:Math.max(e.clientWidth,window.innerWidth||0),height:Math.max(e.clientHeight,window.innerHeight||0)}}();return{top:0,left:0,right:e.width,bottom:e.height}}},{key:"loadIamge",value:function(e){var t=this,n=e.__JUEJIN_LAZYLOAD,i=n.url,o=n.isImg;n.loading=!0,this.updateElementClassByState("loading",e),this.invokeStateHook("loading",i,e),function(e,t,n){if(e){var i=new Image;i.onload=function(){t&&t(e)},i.onerror=function(){n&&n(e)},i.src=e}}(i,function(){o?e.src=i:e.style.backgroundImage="url("+i+")",t.removeElement(e),t.updateElementClassByState("loaded",e),t.invokeStateHook("loaded",i,e)},function(){t.removeElement(e),t.updateElementClassByState("error",e),t.invokeStateHook("error",i,e)})}},{key:"updateElementClassByState",value:function(e,t){switch(e){case"inited":t.classList.add("inited"),t.classList.remove("loading"),t.classList.remove("loaded"),t.classList.remove("error");break;case"loading":t.classList.add("loading");break;case"loaded":t.classList.remove("loading"),t.classList.add("loaded");break;case"error":t.classList.remove("loading"),t.classList.add("error")}}},{key:"invokeStateHook",value:function(e,t,n){this.options.onStateChange&&this.options.onStateChange(e,t,n,this)}},{key:"destroy",value:function(){this.removeEventListener(),this.clean(),this.setOptions({})}}]),r}()}); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.JuejinLazyload=t()}(this,function(){"use strict";function e(e,t,n){var i=arguments.length>3&&void 0!==arguments[3]&&arguments[3];return e.addEventListener(t,n,i),function(){e.removeEventListener(t,n,i)}}!function(){function e(e){this.value=e}function t(t){function n(o,r){try{var a=t[o](r),s=a.value;s instanceof e?Promise.resolve(s.value).then(function(e){n("next",e)},function(e){n("throw",e)}):i(a.done?"return":"normal",a.value)}catch(e){i("throw",e)}}function i(e,t){switch(e){case"return":o.resolve({value:t,done:!0});break;case"throw":o.reject(t);break;default:o.resolve({value:t,done:!1})}(o=o.next)?n(o.key,o.arg):r=null}var o,r;this._invoke=function(e,t){return new Promise(function(i,a){var s={key:e,arg:t,resolve:i,reject:a,next:null};r?r=r.next=s:(o=r=s,n(e,t))})},"function"!=typeof t.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)}}();var t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},n=function(){function e(e,t){for(var n=0;n=t&&(n=Date.now(),setTimeout(function(){e.apply(null,o)},t))}}(function(){return t.updateState()},this.options.interval);this.removeScrollEventListener=e(window,"scroll",n),this.removeResizeEventListener=e(window,"resize",n)}}},{key:"removeEventListener",value:function(){this.removeScrollEventListener&&this.removeScrollEventListener(),this.removeResizeEventListener&&this.removeResizeEventListener()}},{key:"addOrUpdateElement",value:function(e){var t=this.getElementList(e),n=t.filter(function(e){return!e.__JUEJIN_LAZYLOAD});this.elementList=(this.elementList||[]).concat(n),t.forEach(this.initElement.bind(this)),this.updateState()}},{key:"removeElement",value:function(e){var t=this.getElementList(e);this.elementList=this.elementList.filter(function(e){return-1===t.indexOf(e)}),t.forEach(this.removeInfo.bind(this))}},{key:"clean",value:function(){this.elementList.forEach(this.removeInfo.bind(this)),this.elementList=[]}},{key:"getElementList",value:function(e){return e?"string"==typeof e?[].slice.call(document.querySelectorAll(e)):"number"==typeof e.length?[].slice.call(e):[e]:[]}},{key:"initElement",value:function(e){var t=this.options.infoGetter&&this.options.infoGetter(e),n=i({},t,{isImg:"IMG"===e.nodeName,loading:!1});n.hasPlaceholder=n.isImg&&n.width&&n.height,n.hasPlaceholder&&(e.src=function(e,t){return["data:image/svg+xml;utf8,",'','"].join("")}(n.width,n.height)),e.__JUEJIN_LAZYLOAD=n,this.updateElementClassByState("inited",e),this.invokeStateHook("inited",n.url,e)}},{key:"removeInfo",value:function(e){e.__JUEJIN_LAZYLOAD&&(e.__JUEJIN_LAZYLOAD=null)}},{key:"updateState",value:function(){var e=this;if(this.elementList.length){var t=this.getActiveArea();this.elementList.forEach(function(n){var i=n.__JUEJIN_LAZYLOAD.loading,o=n.getBoundingClientRect(),r=function(e,t){return!(t.bottome.bottom||t.righte.right)}(t,o);!i&&r&&e.loadIamge(n)})}}},{key:"getActiveArea",value:function(){var e=this.getVisibleArea(),t=this.options.threshold||0;return{top:e.top-t,left:e.left-t,right:e.right+t,bottom:e.bottom+t}}},{key:"getVisibleArea",value:function(){if(this.options.visibleAreaGetter)return this.options.visibleAreaGetter();var e=function(){var e=document.documentElement;return{width:Math.max(e.clientWidth,window.innerWidth||0),height:Math.max(e.clientHeight,window.innerHeight||0)}}();return{top:0,left:0,right:e.width,bottom:e.height}}},{key:"loadIamge",value:function(e){var t=this,n=e.__JUEJIN_LAZYLOAD,i=n.url,o=n.isImg;n.loading=!0,this.updateElementClassByState("loading",e),this.invokeStateHook("loading",i,e),function(e,t,n){if(e){var i=new Image;i.onload=function(){t&&t(e)},i.onerror=function(){n&&n(e)},i.src=e}}(i,function(){o?e.src=i:e.style.backgroundImage="url("+i+")",t.removeElement(e),t.updateElementClassByState("loaded",e),t.invokeStateHook("loaded",i,e)},function(){t.removeElement(e),t.updateElementClassByState("error",e),t.invokeStateHook("error",i,e)})}},{key:"updateElementClassByState",value:function(e,t){switch(e){case"inited":t.classList.add("inited"),t.classList.remove("loading"),t.classList.remove("loaded"),t.classList.remove("error");break;case"loading":t.classList.add("loading");break;case"loaded":t.classList.remove("loading"),t.classList.add("loaded");break;case"error":t.classList.remove("loading"),t.classList.add("error")}}},{key:"invokeStateHook",value:function(e,t,n){this.options.onStateChange&&this.options.onStateChange(e,t,n,this)}},{key:"destroy",value:function(){this.removeEventListener(),this.clean(),this.setOptions({})}}]),r}()}); diff --git a/package.json b/package.json index ab7ef2f..1094409 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "juejin-lazyload", - "version": "0.1.3", + "version": "0.1.4", "description": "juejin lazyload", "main": "dist/juejin-lazyload.min.js", "scripts": { diff --git a/src/juejin-lazyload.js b/src/juejin-lazyload.js index 1518f1f..a8727d9 100644 --- a/src/juejin-lazyload.js +++ b/src/juejin-lazyload.js @@ -92,6 +92,7 @@ export default class JuejinLazyload { } element[INFO_PROP_NAME] = info this.updateElementClassByState('inited', element) + this.invokeStateHook('inited', info.url, element) } removeInfo (element) {