1141b4535971651709a47805bbe21d1a3ff2c968dde842e2168a98fcf504f80f6b887ab6057e57c2593ec0cd106ecac427bf9a24f91819f7816e36a71baafc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Watch Size [<img src="https://jonathantneal.github.io/dom-logo.svg" alt="PostCSS" width="90" height="90" align="right">][Watch Size]
  2. [![NPM Version][npm-img]][npm-url]
  3. [![Build Status][cli-img]][cli-url]
  4. [![Support Chat][git-img]][git-url]
  5. [Watch Size] lets you watch the size changes of a DOM element using the
  6. `watchSize` function.
  7. ## Usage
  8. Add [Watch Size] to your project:
  9. ```bash
  10. npm install watch-size
  11. ```
  12. Use `watchSize()` to rewrite your queries:
  13. ```js
  14. import watchSize from 'watch-size';
  15. const stop = watchSize(
  16. document.querySelector('.box:nth-child(2)')
  17. ({ width, height }) => { /* callback */ }
  18. );
  19. /* use stop() to end listening */
  20. ```
  21. ## How does it work?
  22. The `watchSize` function attaches hidden, over-sized DOM elements to your
  23. target with `scroll` listeners that are triggered whenever the element resizes.
  24. This allows the listener to avoid polling, and to truly respond to size changes.
  25. This technique can be used in all browsers going back to Internet Explorer 9.
  26. The entire script is about 400 bytes when minified and gzipped.
  27. [cli-img]: https://img.shields.io/travis/jonathantneal/watch-size.svg
  28. [cli-url]: https://travis-ci.org/jonathantneal/watch-size
  29. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  30. [git-url]: https://gitter.im/postcss/postcss
  31. [npm-img]: https://img.shields.io/npm/v/watch-size.svg
  32. [npm-url]: https://www.npmjs.com/package/watch-size
  33. [Watch Size]: https://github.com/jonathantneal/watch-size