项目作者: hustcc

项目描述 :
:herb: 1Kb DOM element size sensor which will callback when size changed.
高级语言: JavaScript
项目地址: git://github.com/hustcc/size-sensor.git
创建时间: 2018-06-06T01:52:49Z
项目社区:https://github.com/hustcc/size-sensor

开源协议:

下载


size-sensor

DOM element size sensor which will callback when the element size changed.

DOM 元素尺寸监听器,当元素尺寸变化的时候,将会触发回调函数!

Build Status
npm
npm
gzip

Install

npm i —save size-sensor

Then import it.

  1. import { bind, clear } from 'size-sensor';

or import it by script in HTML, then get sizeSensor on window.

  1. <script src="https://unpkg.com/size-sensor/dist/size-sensor.min.js"></script>

Usage

  • bind & unbind
  1. import { bind, clear } from 'size-sensor';
  2. // bind the event on element, will get the `unbind` function
  3. const unbind1 = bind(document.querySelector('.container'), element => {
  4. // do what you want to to.
  5. });
  6. const unbind2 = bind(document.querySelector('.container'), element => {
  7. // do what you want to to.
  8. });
  9. // if you want to cancel bind event.
  10. unbind1();
  • clear
  1. import { bind, clear } from 'size-sensor';
  2. /*
  3. * // bind the resize event.
  4. * const unbind1 = bind(...);
  5. * const unbind2 = bind(...);
  6. * ...
  7. */
  8. // you can cancel all the event of element.
  9. clear(element);

API

There is only 2 API:

  • bind(element, callback)

Bind the resize trigger function on element. The function will return unbind function.

  • clear(element)

Clear all the object and resize event on element.

Strategies

The size sensor strategies include:

  • ResizeObserver: use resizeObserver to observe element’s size.
  • object: use object document’s resize event.

If ResizeObserver exists, use it, else use object as default.

Let me know

Online demo click here. Used By:

License

ISC@hustcc.