You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
3.6 KiB

!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,o=r&&navigator.userAgent.toLowerCase(),s=(o&&/wechatdevtools/.test(o),o&&o.indexOf("android"),function(){if("string"!=typeof o)return;var t=/os (\d\d?_\d(_\d)?)/.exec(o);if(!t)return;var e=t[1].split("_").map(function(t){return parseInt(t,10)});13<=e[0]&&e[1]}(),r&&document.createElement("div").style),e=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 t(t){return!1===e?t:"standard"===e?"transitionEnd"===t?"transitionend":t:e+t.charAt(0).toUpperCase()+t.substr(1)}e&&"standard"!==e&&e.toLowerCase();var n=t("transform"),i=t("transition");r&&t("perspective")in s,t("transitionTimingFunction"),t("transitionDuration"),t("transitionDelay"),t("transformOrigin"),t("transitionEnd"),t("transitionProperty");function a(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 h(t){this.scroll=t,this.stopObserver=!1,this.init()}return h.prototype.init=function(){this.handleMutationObserver(),this.handleHooks()},h.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()},h.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()})},h.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))}},h.prototype.startObserve=function(t){t.observe(this.scroll.scroller.content,{attributes:!0,childList:!0,subtree:!0})},h.prototype.shouldNotRefresh=function(){var t=this.scroll.scroller,e=t.scrollBehaviorX,o=t.scrollBehaviorY,r=e.currentPos>e.minScrollPos||e.currentPos<e.maxScrollPos||o.currentPos>o.minScrollPos||o.currentPos<o.maxScrollPos;return t.animater.pending||r},h.prototype.checkDOMUpdate=function(){var o=this,r=this.scroll.scroller.content,s=a(r),n=s.width,i=s.height,h=function(){setTimeout(function(){var t,e;o.stopObserver||(t=(s=a(r)).width,e=s.height,n===t&&i===e||o.scroll.refresh(),n=t,i=e,h())},1e3)};h()},h.prototype.registerHooks=function(t,e,o){t.on(e,o,this),this.hooksFn.push([t,e,o])},h.prototype.stopObserve=function(){this.stopObserver=!0,this.observer&&this.observer.disconnect()},h.prototype.destroy=function(){this.stopObserve(),this.hooksFn.forEach(function(t){var e=t[0],o=t[1],r=t[2];e.off(o,r)}),this.hooksFn.length=0},h.pluginName="observeDOM",h});