Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect CSS3 resize events

The CSS3 resize property can be assigned to arbitrary elements. I'm looking for a way to detect such a resize on, say, divs (I don't mind it only working in Firefox at the moment):

div {   resize: horizontal;   overflow: hidden; } 

Unfortunately, the onresize event seems not to be fired on the div. How can I detect in JavaScript when such a user-instantiated resize has happened?

Edit: FWIW I had opened a bug report over at Mozilla. If you want to track it: https://bugzilla.mozilla.org/show_bug.cgi?id=701648

like image 921
Boldewyn Avatar asked Nov 10 '11 16:11

Boldewyn


People also ask

How do I capture a Windows resize event?

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.

What happens 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.

How do I resize an element in css3?

The resize property defines if (and how) an element is resizable by the user. Note: The resize property does not apply to inline elements or to block elements where overflow="visible". So, make sure that overflow is set to "scroll", "auto", or "hidden".


1 Answers

Resizing is like a style change. As such it can be observed with a MutationObserver. The more specific ResizeObserver is probably even better:

let observer = new ResizeObserver(function(mutations) {   console.log('mutations:', mutations); });  let child = document.querySelector('textarea'); observer.observe(child, { attributes: true });
<textarea></textarea>
like image 118
Daniel Darabos Avatar answered Sep 22 '22 09:09

Daniel Darabos