34

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Boldewyn
  • 81,211
  • 44
  • 156
  • 212
  • @DaveL nope, that only works for the `window` object. – Felix Nov 10 '11 at 16:27
  • from what i know, the `onresize` event only works with `window`. Does the click event fire when you resize? Maybe you could use that ( to recalculate width/height when clicked) – alinn Nov 10 '11 at 16:28
  • I could detect mousemove over the div but was hoping for a more comfortable solution. – Boldewyn Nov 10 '11 at 16:30
  • For me overflow:hidden on body turned off resize event on FF and Chrome. Same issue? – Dan Froberg Jun 06 '23 at 19:48

6 Answers6

28

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);
<textarea></textarea>
Daniel Darabos
  • 26,991
  • 10
  • 102
  • 114
  • Indeed. Good idea! – Boldewyn Jun 28 '17 at 20:52
  • Doesn't seem to work in Safari 10, even with `WebKitMutationObserver`. – Dominic K Aug 10 '17 at 22:08
  • 2
    Looks like this answer is outdated too, and you should be using [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) today instead. – phil294 Oct 27 '21 at 16:04
  • Thanks! The snippet with `MutationObserver` still works for me in Chrome 95. But `ResizeObserver` works too. I've updated the answer. Do you see any indication that `MutationObserver` will stop working in the future? I don't remember where I used it... – Daniel Darabos Oct 28 '21 at 19:15
  • Note that `attributes: true` is not relevant to `ResizeObserver()`. [Specs](https://drafts.csswg.org/resize-observer/#enumdef-resizeobserverboxoptions) – thdoan Jun 21 '23 at 01:44
  • Fixed, thank you! – Daniel Darabos Jun 21 '23 at 10:55
12

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

Community
  • 1
  • 1
robertc
  • 74,533
  • 18
  • 193
  • 177
  • Cool, that's in any case way better than mouseover catching. Thanks! – Boldewyn Nov 11 '11 at 09:41
  • 4
    Doesn't fire in Chrome, Linux – Alp Aug 27 '12 at 11:13
  • 5
    @Alp I believe DOM2 Mutation Events have been [replaced by DOM4 Mutation Observers](https://code.google.com/p/chromium/issues/detail?id=142648) in recent builds. [Try this article](http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers). – robertc Aug 27 '12 at 12:36
  • 2
    @robertc I dont think they work with resize https://code.google.com/p/chromium/issues/detail?id=293948 – shuji Nov 16 '13 at 20:53
  • 3
    Thanks @robertc! The mentioned issue is now fixed and MutationObserver worked for me. I've added it as an answer. – Daniel Darabos Jun 28 '17 at 15:40
  • 1
    @Boldewyn, somewhere between robertc's nice answer/comment and today, many years later, this has been deprecated: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Mutation_events Care to accept one of the other answers for future readers? – Arjan Dec 26 '17 at 12:28
  • 1
    http://jsfiddle.net/leaverou/pckrf/ shows that today, years later, Firefox still supports this, but Chrome and Safari do not. – Arjan Dec 26 '17 at 12:38
  • When you're handling `document.body` events for elements with `resize` CSS property, beware of this Chrome-specific edge case: https://bugs.chromium.org/p/chromium/issues/detail?id=1456570 – thdoan Jun 21 '23 at 01:29
7

Since the resize event clearly doesn't work (currently, at least), you can try one of these alternative options:

  1. Use a combination of mousedown, mousemove and/or mouseup to tell whether the div is being / has been resized. If you want really fine-grained control you can check in every mousemove event how much / if the div has been resized. If you don't need that, you can simply not use mousemove at all and just measure the div in mousedown and mouseup and figure out if it was resized in the latter.
  2. Poll every 200ms or so (depending on your needs) and compare the current size with the last known size. See setTimeout().
Felix
  • 88,392
  • 43
  • 149
  • 167
  • Yes, polling and mousemove are valid options, thanks. I thought of them myself. +1 for them. robertc's answer seems more elegant, though. – Boldewyn Nov 11 '11 at 09:42
3

You can use the ResizeSensor class of the css-element-queries polyfill from

https://github.com/marcj/css-element-queries

It allows you to call a javascript function on size changes for all types of elements, not only for window. It sets up a real sensor, not a javascript setTimeout poll.

Use it like this:

new ResizeSensor($('#myelement'), function() {
    console.log("myelement's size has changed");
});

Supported browsers are: all incl. IE6+.

Marc J. Schmidt
  • 8,302
  • 4
  • 34
  • 33
0

This seemed to work pretty well for me:

$("body").on('mousedown mousemove', ".resizeItem", function () {
    console.log($(this).height());
})

"Use a combination of mousedown, mousemove and/or mouseup"

as per Felixs answer.

Especially useful when combining a custom search result panel with Dev Extremes Scroll View and you want full control over it.

$("body").on('mousedown mousemove', ".scrollContainer", function () {
    var h = $(this).height() - 20;
    $(".scrollArea").dxScrollView('instance').option('height', h);
});
-2

According to this site it only works in internet explorer 9.

Try this fiddle: http://jsfiddle.net/maniator/3Zva3/

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • I thought so, that onresize should fire in this case. I go looking, if there's a bug report over at bugzilla. – Boldewyn Nov 11 '11 at 09:43
  • That MSDN page is for the `window.resize` event - which fires when you resize the browser window, not an element resize event, when you resize a tag with a css3 resize property through dragging a handle. – Andrew Oct 13 '21 at 22:13