UNPKG

@better-scroll/observe-dom

Version:

Dynamic recalculating container's size for BetterScroll

2 lines (1 loc) 3.77 kB
!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 s="undefined"!=typeof window,t=s&&navigator.userAgent.toLowerCase();if(t&&/wechatdevtools/.test(t),t&&t.indexOf("android"),"string"==typeof t)(t=/os (\d\d?_\d(_\d)?)/.exec(t))&&(13===(t=t[1].split("_").map(function(t){return parseInt(t,10)}))[0]&&t[1]);if(s)try{var e={};Object.defineProperty(e,"passive",{get:function(){}}),window.addEventListener("test-passive",function(){},e)}catch(t){}var r=s&&document.createElement("div").style,o=function(){if(s)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!==r[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 a(t){var e;return t instanceof window.SVGElement?{top:(e=t.getBoundingClientRect()).top,left:e.left,width:e.width,height:e.height}:{top:t.offsetTop,left:t.offsetLeft,width:t.offsetWidth,height:t.offsetHeight}}function i(t){this.scroll=t,this.stopObserver=!1,this.init()}return s&&n("perspective")in r,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 s=!1,r=!1,n=0;n<t.length;n++){var i=t[n];if("attributes"!==i.type){s=!0;break}if(i.target!==this.scroll.scroller.content){r=!0;break}}s?this.scroll.refresh():r&&(clearTimeout(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,e=e.currentPos>e.minScrollPos||e.currentPos<e.maxScrollPos||o.currentPos>o.minScrollPos||o.currentPos<o.maxScrollPos;return t.animater.pending||e},i.prototype.checkDOMUpdate=function(){var o=this,s=this.scroll.scroller.content,r=a(s),n=r.width,i=r.height,h=function(){setTimeout(function(){var t,e;o.stopObserver||(t=(r=a(s)).width,e=r.height,n===t&&i===e||o.scroll.refresh(),n=t,i=e,h())},1e3)};h()},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});