Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize event not triggered on div

Take a look at this example: https://jsfiddle.net/vxun2Lgg/2/

I've attached an "resize" event listener on container div. After opening up dev tools, and modifying the width of container, resize callback does not get called. What am I missing?

PS: I am not interested in window resize event, only in container div.

var container = document.getElementsByClassName("container")[0];
container.addEventListener("resize", function() {
  console.log("resizing")
});
<div class="container"></div>
like image 473
sanjihan Avatar asked Mar 25 '18 12:03

sanjihan


People also ask

How to listen resize event JavaScript?

Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.

How to handle window resize in JavaScript?

The onresize event occurs when the browser window has been resized. Tip: To get the size of an element, use the clientWidth, clientHeight, innerWidth, innerHeight, outerWidth, outerHeight, offsetWidth and/or offsetHeight properties.

What happen when the window is resized?

The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble. In some earlier browsers it was possible to register resize event handlers on any HTML element.


2 Answers

This answer extends the accepted answer by providing a runnable code in pure JavaScript that you can try on the snippet runner (button at the bottom). I kept the explanations directly on the code.

Believe or not, you only need a 3-line function (the onresize function) to simulate the resize event.

Enjoy it!

// This function works like an Event Listener for
// resizing a dom element.
//
// The ResizeObserver calls the callback function
//   whenever the size of the dom_element changes
//   (because it's "observing" the dom_elem)
//
// Do this:
//
//    understand_it_first
//       .then(copy_it)
//       .then(use_it);
//
// THIS IS THE 3-LINE FUNCION YOU WANT TO USE :)
//
const onresize = (dom_elem, callback) => {
  const resizeObserver = new ResizeObserver(() => callback() );
  resizeObserver.observe(dom_elem);
};

// USING IT
//

// Using constants to make it easier to read the code
//
const bb = document.getElementById('bad_boss');
const be = document.getElementById('bad_employee');

// Finally, register the observer for the dom_elem
//
onresize(bb, function () {
  be.style.width  = bb.offsetWidth + 'px';
  be.style.height = bb.offsetHeight + 'px';
});
#bad_boss, #bad_employee{
  font-size        : 32px;
  height           : 200px;
  width            : 200px;
}
  
/* Colors that Talk*/

#bad_boss {
  background-color : #0badb055;
}
#bad_employee {
  background-color : #0b00c0de;
  color            : #0badc0de;
}
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>ResizeObserver - onresize Demo</title>
    </head>
    <body>
        <!-- Experiment to resize this <textarea> -->
        <textarea id="bad_boss">Always do whatever I say!</textarea>
        <div id="bad_employee">Always, Boss!</div>
    </body>
</html>
like image 187
Almir Campos Avatar answered Sep 16 '22 13:09

Almir Campos


resize is only valid for the window. If supported you can use ResizeObserver.

new ResizeObserver(() => console.log("resizing")).observe(container);

Otherwise, you will probably have to poll using setInterval and check the size.

like image 27
H.B. Avatar answered Sep 17 '22 13:09

H.B.