Giter Site home page Giter Site logo

simple-element-resize-detector's Introduction

simple-element-resize-detector NPM

Observes resizing of an element using a hidden iframe.

JSFiddle Demo

Installation

npm i -S simple-element-resize-detector

Usage

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
	});
});

To stop observing resize events, simply remove the returned detector frame:

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

detector.remove();

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

Notes

  • element passed to observeResize() must have position: relative style to be correctly observed, otherwise nearest relative ancestor will be observed instead.
  • This library uses <iframe>s to detect when an element resizes. <iframe>s are heavy objects are usually take good amount of memory. Be careful and don't abuse it.

License

MIT

simple-element-resize-detector's People

Contributors

bfred-it avatar developit avatar nekr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simple-element-resize-detector's Issues

Resize detection only works in Chrome now

I understand that at this point, this project seems to be no longer maintained. But I just want to point out, the code seems only work in Chrome (8/2019), which makes it a bit pointless because Chrome natively supports ResizeObserver. Firefox 68 and Edge (the non-chromium one) currently doesn't support ResizeObserver and this workaround also doesn't work with the two browsers. Here is my code in case I used in incorrectly:

<!doctype html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Why it doesn't work</title>
</head>

<body>
  <div class='dummy'>This is the content that will be resized.</div>
  <style>
    .dummy {
      width: 100%;
      background: red;
      position: relative;
    }
  </style>
  <script>
    const CSS = `position:absolute;left:0;top:-100%;width:100%;height:100%;margin:1px 0 0;border:none;opacity:0;pointer-events:none;`;

    function observe(element, handler) {
      const frame = document.createElement('iframe');
      const supportsPE =
        document.documentMode < 11 && 'pointerEvents' in frame.style;

      frame.style.cssText = `${CSS}${supportsPE ? '' : 'visibility:hidden;'}`;
      frame.onload = () => {
        frame.contentWindow.onresize = () => {
          handler(element);
        };
      };
      element.appendChild(frame);
      return frame;
    };

    observe(document.querySelector('.dummy'), function () {
      console.log('dummy: resized!');
    });
  </script>
</body>

</html>

Possible perf suggestion

I wrote one of these that used the same methodology many years ago and found that object elements with no navigation context were much lighter on memory than iframes. It has been so long I am not sure if this is still the case, so take this suggestion with a grain of salt.

Don't work in firefox

It don't work in Firefox, the event is not fired when there is visibility:hidden. If you remove it it's fired.

Resize events only work if iframe in viewport

The solution of top:-100%; and margin:1px 0 0; does not keep the iframe positioned in a viewable area at all times. If the target element is nested in an element with overflow: hidden and the target element has a negative top margin or a negative absolute positioned top the resize events stop working.

top: 0; z-index: -1 seems to fix the issue but I'm not sure what all the consequences are.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.