How to Detect CSS3 Resize Events

How to detect CSS3 resize events

Listen to DOMAttrModified events. Got the idea from this answer, this jsFiddle appears to work in Firefox 8 (if you open the console).

Detect When User Resizes Div With CSS resize: both

You can use MutationObserver for this. Source here.

let observer = new MutationObserver(function(mutations) {
console.log('Resized');
});

let child = document.querySelector('div');
observer.observe(child, { attributes: true });
div {
resize: both;
overflow: auto;
border: 1px solid;
width: 200px;
height: 200px;

}
<div>
Hi!
</div>

How to detect DIV's dimension changed?

There is a very efficient method to determine if a element's size has been changed.

http://marcj.github.io/css-element-queries/

This library has a class ResizeSensor which can be used for resize detection.
It uses an event-based approach, so it's damn fast and doesn't waste CPU time.

Example:

new ResizeSensor(jQuery('#divId'), function(){ 
console.log('content dimension changed');
});

Please do not use the jQuery onresize plugin as it uses setTimeout() in combination with reading the DOM clientHeight/clientWidth properties in a loop to check for changes.
This is incredible slow and inaccurate since it causes layout thrashing.

Disclosure: I am directly associated with this library.

How to detect resize of any element in HTML5

As of July 2020, ResizeObserver is still un-official in W3C nor WhatWG but it is already supported by all major browsers since support Safari 13.1 since 2020-Mar-24.


FYI, there's a spec for a new ResizeObserver API. Chrome seems to be the only browser that has implemented it as of Aug 2018 (see caniuse.com), but there's at least one polyfill you can use now (which uses MutationObserver).

Can I detect browser resize events?

$(window).resize(function() {
// handle accordingly
});

Onresize not triggering on resize

The resize event is fired when the document (not any element) is resized.
W3schools is not clear on that.
The CSS resize property does not fire resize events.

So this is not the event you are looking for. See Connum's comment for details on resizing your div.



Related Topics



Leave a reply



Submit