Optimized cross-browser resize listener for elements.
Installations
npm install element-resize-detector
Releases
Unable to fetch releases
Developer
wnr
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
12.20.0
NPM Version
6.14.8
Statistics
1,319 Stars
332 Commits
118 Forks
21 Watching
1 Branches
19 Contributors
Updated on 14 Nov 2024
Bundle Size
17.31 kB
Minified
5.64 kB
Minified + Gzipped
Languages
JavaScript (99.3%)
HTML (0.7%)
Total Downloads
Cumulative downloads
Total Downloads
564,565,732
Last day
-25.9%
153,924
Compared to previous day
Last week
-4.6%
1,004,220
Compared to previous week
Last month
1.5%
4,513,546
Compared to previous month
Last year
-40.8%
66,008,307
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
element-resize-detector
Optimized cross-browser resize listener for elements. Up to 37x faster than related approaches (read section 5 of the article).
npm install element-resize-detector
Usage
Include the script in the browser:
1<script src="node_modules/element-resize-detector/dist/element-resize-detector.min.js"></script>
This will create a global function elementResizeDetectorMaker
, which is the maker function that makes an element resize detector instance.
You can also require
it like so:
1var elementResizeDetectorMaker = require("element-resize-detector");
Create instance
1// With default options (will use the object-based approach).
2var erd = elementResizeDetectorMaker();
3
4// With the ultra fast scroll-based approach.
5// This is the recommended strategy.
6var erdUltraFast = elementResizeDetectorMaker({
7 strategy: "scroll" //<- For ultra performance.
8});
There is also an callOnAdd
option, which determines if listeners should be called when they are getting added. Default is true. If true, the listener is guaranteed to be called when it has been added. If false, the listener will not be guarenteed to be called when it has been added (does not prevent it from being called).
API
listenTo(element, listener) or listenTo(options, element, listener)
Listens to the element for resize events and calls the listener function with the element as argument on resize events. Options passed to the function will override the instance options.
Example usage:
1erd.listenTo(document.getElementById("test"), function(element) { 2 var width = element.offsetWidth; 3 var height = element.offsetHeight; 4 console.log("Size: " + width + "x" + height); 5});
removeListener(element, listener)
Removes the listener from the element.
removeAllListeners(element)
Removes all listeners from the element, but does not completely remove the detector. Use this function if you may add listeners later and don't want the detector to have to initialize again.
uninstall(element)
Completely removes the detector and all listeners.
initDocument(document)
If you need to listen to elements inside another document (such as an iframe), you need to init that document with this function. Otherwise the library won't be able to detect when elements are attached to the document. So for an iframe, simpy invoke erd.initDocument(iframe.contentDocument);
when the iframe is mounted on the DOM for the first time. The document from which the element resize detector instance is created will be initialized automatically. Notice that a new document is created when an iframe loads its content. So for iframes, be sure you invoke this function for each onLoad
iframe event.
Caveats
- If the element has
position: static
it will be changed toposition: relative
. Any unintentionaltop/right/bottom/left/z-index
styles will therefore be applied and absolute positioned children will be positioned relative to the element. - A hidden element will be injected as a direct child to the element.
Credits
Big thanks to Evry sponsoring this project.
This library is using the two approaches (scroll and object) as first described at http://www.backalleycoder.com/2013/03/18/cross-browser-event-based-element-resize-detection/.
The scroll based approach implementation was based on Marc J's implementation https://github.com/marcj/css-element-queries/blob/master/src/ResizeSensor.js.
Please note that both approaches have been heavily reworked for better performance and robustness.
Changelog
1.2.4
- Harden onExpandScroll and onShrinkScroll handlers. See #132
1.2.3
- Fix problems with object approach in FF. Revert #122.
1.2.2
- Fixes scroll strategy to account for elements within shadow root. See #127.
- Fix potential contenteditable bugs with object approach. See #122.
1.2.1
A release that includes 1.1.15 and 1.1.16 with 1.2.0.
1.2.0
- Add new method
initDocument(document)
which is needed when listening to detached elements in other documents, such as iframes. - Add a new optional option that adds
important!
to most style properties, to avoid CSS overriding. Disabled by default. - Fix an issue with the object approach in IE8. See #95.
- Fix uninstall issue with object approach. See #102.
- Fixed errornous optimization that prevented scrollbar repositioning for really fast x -> y -> x resizes.
1.1.16
- Fix bug that could happen during uninstall when waiting for unrendered objects. See #117.
1.1.15
- ADA compliance fix for object approach. See #105.
1.1.14
- Explicit use of window.getComputedStyle everywhere.
1.1.13
- Only notify listeners when actual size change happened (in the rare case when multiple scroll events happens for the same resize). See #86.
1.1.12
- Fixed an issue with embedded WebView's on Android and iOS (when getComputedStyle.width = null). See #74.
- Fixed an issue with unrendered iframe in FireFox. See #68.
1.1.11
- Cleaned up the development build tools.
- Updated dev dependencies.
- Fixed an issue when uninstalling an element, and then calling listenTo in the middle of an old resize event. See #61.
1.1.10
- Fixed so that injected scroll elements are
flex: none
. See #64. - Fixed so that injected object element is not focusable. See #67.
1.1.9
- Fixed uninstall issue when
callOnAdd
being true. Also now removingonAnimationStart
listener when uninstalling. See #49.
1.1.8
- Fixed a compatability issue with
options.idHandler.get
.
1.1.7
- Fixed some rare issues with uninstalling elements while preparing/resizing.
1.1.6
- Fixed an issue with the resize detector changing the dimensions of the target element in some browsers (e.g., IE and FireFox).
1.1.5
- Fixed an issue with having parent elements
dir=RTL
.
1.1.4
- Added extra safety styles to injected elements to make them more resilient to global CSS affecting them.
1.1.3
- Now
uninstall
supports being called with elements that haven't been initialized.uninstall
simply ignores non-erd elements. uninstall
now also supports a collection of elements.
1.1.2
- Fixed so that
uninstall
may be called directly after alistenTo
call. - Fixed a typo in the readme.
- Fixed an invalid test.
1.1.1
- Using
window.getComputedStyle
instead of relying on the method being available in the global scope. This enables this library to be used in simulated browser environments such as jsdom.
1.1.0
- Supporting inline elements
- Event-based solution for detecting attached/rendered events so that detached/unrendered elements can be listened to without polling
- Now all changes that affects the offset size of an element are properly detected (such as padding and font-size).
- Scroll is stabilized, and is the preferred strategy to use. The object strategy will be deprecated (and is currently only used for some legacy browsers such as IE9 and Opera 12).
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 6/30 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 6 are checked with a SAST tool
Reason
82 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-wg6g-ppvx-927h
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-vh7m-p724-62c2
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-957j-59c2-j692
- Warn: Project is vulnerable to: GHSA-m5pj-vjjf-4m3h
- Warn: Project is vulnerable to: GHSA-j383-35pm-c5h4
- Warn: Project is vulnerable to: GHSA-rm36-94g8-835r
- Warn: Project is vulnerable to: GHSA-hcj4-xf6x-63wj
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-6x33-pw7p-hmpq
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-6c3j-c64m-qhgq
- Warn: Project is vulnerable to: GHSA-gxr4-xjj5-5px2
- Warn: Project is vulnerable to: GHSA-jpcq-cgw6-v4j6
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-7x7c-qm48-pq9c
- Warn: Project is vulnerable to: GHSA-rc3x-jf5g-xvc5
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-82v2-mx6x-wq7q
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-q75g-2496-mxpp
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-fxwf-4rqh-v8g3
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-xfhh-g9f5-x4m4
- Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-v2p6-4mp7-3r9v
- Warn: Project is vulnerable to: GHSA-mgfv-m47x-4wqp
- Warn: Project is vulnerable to: GHSA-5v72-xg48-5rpm
- Warn: Project is vulnerable to: GHSA-72mh-269x-7mh5
- Warn: Project is vulnerable to: GHSA-h4j5-c7cj-74xg
Score
2
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to element-resize-detector
@types/element-resize-detector
TypeScript definitions for element-resize-detector
@types/simple-element-resize-detector
TypeScript definitions for simple-element-resize-detector
vue-element-resize-detector
vue directive for element-resize-detector
react-container-dimensions
Wrapper component that detects element resize and passes new dimensions down the tree. Based on [element-resize-detector](https://github.com/wnr/element-resize-detector)