使用隐藏的iframe观察元素的大小调整 - simple-element-resize-detector

2017-01-04      149      JavaScript
项目简介

simple-element-resize-detector

使用一个隐藏的iframe观察元素的大小调整.

安装

npm i -S simple-element-resize-detector

使用方法

import observeResize from 'simple-element-resize-detector';

// any DOM element that can have children
let element = document.createElement('div');

// listen for resize
observeResize(element, () => {
    console.log('new size: ', {
        width: element.clientWidth,
        height: element.clientHeight
    });
});

为了停止观察大小调整时间,只需移除返回检测器框架:

let detector = observeResize(el, () => {});

detector.remove();

// or, for better browser compatibility:
// detector.parentNode.removeChild(detector)