dom-size-monitor
Version:
监听元素大小变化
2 lines (1 loc) • 2.21 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e["dom-size-monitor"]={})}(this,function(e){"use strict";var t=1,n=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:30,n=null;return function(){for(var i=this,o=arguments.length,r=Array(o),u=0;u<o;u++)r[u]=arguments[u];clearTimeout(n),n=setTimeout(function(){e.apply(i,r)},t)}},i="undefined"!=typeof ResizeObserver?function(e){var t=null,i=[],o=n(function(){i.forEach(function(t){t(e)})}),r=function(){t.disconnect(),i=[],t=void 0};return{element:e,bind:function(n){t||(t=function(){var t=new ResizeObserver(o);return t.observe(e),o(),t}()),-1===i.indexOf(n)&&i.push(n)},destroy:r,unbind:function(e){var n=i.indexOf(e);-1!==n&&i.splice(n,1),0===i.length&&t&&r()}}}:function(e){var t=null,i=[],o=n(function(){i.forEach(function(t){t(e)})}),r=function(){t&&t.parentNode&&(t.contentDocument&&t.contentDocument.defaultView.removeEventListener("resize",o),t.parentNode.removeChild(t),t=void 0,i=[])};return{element:e,bind:function(n){t||(t=function(){"static"===getComputedStyle(e).position&&(e.style.position="relative");var t=document.createElement("object");return t.onload=function(){t.contentDocument.defaultView.addEventListener("resize",o),o()},t.setAttribute("style","display:block;position:absolute;top:0;left:0;height:100%;width:100%;transform:translate3d(0,0,0);-webkit-transform:translate3d(0,0,0);overflow:hidden;pointer-events:none;z-index:-1;opacity:0"),t.setAttribute("class","size-sensor-object"),t.type="text/html",e.appendChild(t),t.data="about:blank",t}()),-1===i.indexOf(n)&&i.push(n)},destroy:r,unbind:function(e){var n=i.indexOf(e);-1!==n&&i.splice(n,1),0===i.length&&t&&r()}}},o={},r=function(e){var n=e.getAttribute("size-sensor-id");if(n&&o[n])return o[n];var r=""+t++;e.setAttribute("size-sensor-id",r);var u=i(e);return o[r]=u,u};e.bind=function(e,t){var n=r(e);return n.bind(t),function(){n.unbind(t)}},e.clear=function(e){!function(e){var t=e.element.getAttribute("size-sensor-id");e.element.removeAttribute("size-sensor-id"),e.destroy(),t&&o[t]&&delete o[t]}(r(e))},Object.defineProperty(e,"__esModule",{value:!0})});