UNPKG

hc-components-test

Version:

基于react的通用组件库

80 lines (70 loc) 1.96 kB
const w = window, documentElement = document.documentElement, body = document.body; export class AdaptPage { /** * identify in an iframe * see: http://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t */ static inIframe() { try { return window.self !== window.top; } catch (e) { return true; } } setOffsetHeight(offsetHeight) { this._offsetHeight = offsetHeight; } getScreenHeight() { return (w.innerHeight || documentElement.clientHeight || body.clientHeight) - this._offsetHeight; } constructor() { this._offsetHeight = 0; this._isInIframe = AdaptPage.inIframe(); this._watchers = []; this._adapter = () => { clearTimeout(this._timer); this._timer = setTimeout(() => { let watcher; let index = 0; while (watcher = this._watchers[index]) { if (!watcher.el || watcher.el.offsetParent === null) { this ._watchers .splice(index, 1); } else { watcher.fn(watcher.el); index++; } } }, 200); } /** * + add resize * see: http://stackoverflow.com/questions/19014250/reactjs-rerender-on-browser-resize */ window.addEventListener("resize", this._adapter); } onAdapt(el, callback) { this ._watchers .push({el: el, fn: callback}); callback(el, this._isInIframe); } autoAdaptDim(el, setter) { let callback = (el, isInIframe) => { setter(el.offsetHeight - this._offsetHeight, isInIframe, this.getScreenHeight()); } this.onAdapt(el, callback); } autoAdapt(dom, action) { this.autoAdaptDim(dom, (size, isInIframe, screenHeight) => { action(size, screenHeight, isInIframe); }); } dispose(fn) { this._watchers = []; window.removeEventListener("resize", this._adapter); } }