observe-dom.min.js 3.7 KB

1
  1. !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ObserveDom=e()}(this,function(){"use strict";var r="undefined"!=typeof window,e=r&&navigator.userAgent.toLowerCase();e&&/wechatdevtools/.test(e),e&&e.indexOf("android"),function(){if("string"==typeof e){var t=/os (\d\d?_\d(_\d)?)/.exec(e);if(!t)return;t=t[1].split("_").map(function(t){return parseInt(t,10)});13===t[0]&&t[1]}}();if(r)try{var t={};Object.defineProperty(t,"passive",{get:function(){}}),window.addEventListener("test-passive",function(){},t)}catch(t){}var s=r&&document.createElement("div").style,o=function(){if(!r)return!1;for(var t=0,e=[{key:"standard",value:"transform"},{key:"webkit",value:"webkitTransform"},{key:"Moz",value:"MozTransform"},{key:"O",value:"OTransform"},{key:"ms",value:"msTransform"}];t<e.length;t++){var o=e[t];if(void 0!==s[o.value])return o.key}return!1}();function n(t){return!1===o?t:"standard"===o?"transitionEnd"===t?"transitionend":t:o+t.charAt(0).toUpperCase()+t.substr(1)}o&&"standard"!==o&&o.toLowerCase();n("transform"),n("transition");function h(t){if(t instanceof window.SVGElement){var e=t.getBoundingClientRect();return{top:e.top,left:e.left,width:e.width,height:e.height}}return{top:t.offsetTop,left:t.offsetLeft,width:t.offsetWidth,height:t.offsetHeight}}function i(t){this.scroll=t,this.stopObserver=!1,this.init()}return r&&n("perspective")in s,n("transitionTimingFunction"),n("transitionDuration"),n("transitionDelay"),n("transformOrigin"),n("transitionEnd"),n("transitionProperty"),i.prototype.init=function(){this.handleMutationObserver(),this.handleHooks()},i.prototype.handleMutationObserver=function(){var e=this;"undefined"!=typeof MutationObserver?(this.observer=new MutationObserver(function(t){e.mutationObserverHandler(t,0)}),this.startObserve(this.observer)):this.checkDOMUpdate()},i.prototype.handleHooks=function(){var t=this;this.hooksFn=[],this.registerHooks(this.scroll.hooks,this.scroll.hooks.eventTypes.contentChanged,function(){t.stopObserve(),t.handleMutationObserver()}),this.registerHooks(this.scroll.hooks,this.scroll.hooks.eventTypes.enable,function(){t.stopObserver&&t.handleMutationObserver()}),this.registerHooks(this.scroll.hooks,this.scroll.hooks.eventTypes.disable,function(){t.stopObserve()}),this.registerHooks(this.scroll.hooks,this.scroll.hooks.eventTypes.destroy,function(){t.destroy()})},i.prototype.mutationObserverHandler=function(t,e){var o=this;if(!this.shouldNotRefresh()){for(var r=!1,s=!1,n=0;n<t.length;n++){var i=t[n];if("attributes"!==i.type){r=!0;break}if(i.target!==this.scroll.scroller.content){s=!0;break}}r?this.scroll.refresh():s&&(clearTimeout(e),e=window.setTimeout(function(){o.shouldNotRefresh()||o.scroll.refresh()},60))}},i.prototype.startObserve=function(t){t.observe(this.scroll.scroller.content,{attributes:!0,childList:!0,subtree:!0})},i.prototype.shouldNotRefresh=function(){var t=this.scroll.scroller,e=t.scrollBehaviorX,o=t.scrollBehaviorY,o=e.currentPos>e.minScrollPos||e.currentPos<e.maxScrollPos||o.currentPos>o.minScrollPos||o.currentPos<o.maxScrollPos;return t.animater.pending||o},i.prototype.checkDOMUpdate=function(){var o=this,r=this.scroll.scroller.content,s=h(r),n=s.width,i=s.height,a=function(){setTimeout(function(){var t,e;o.stopObserver||(t=(s=h(r)).width,e=s.height,n===t&&i===e||o.scroll.refresh(),n=t,i=e,a())},1e3)};a()},i.prototype.registerHooks=function(t,e,o){t.on(e,o,this),this.hooksFn.push([t,e,o])},i.prototype.stopObserve=function(){this.stopObserver=!0,this.observer&&this.observer.disconnect()},i.prototype.destroy=function(){this.stopObserve(),this.hooksFn.forEach(function(t){var e=t[0],o=t[1],t=t[2];e.off(o,t)}),this.hooksFn.length=0},i.pluginName="observeDOM",i});