40

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

4
  • @DaveL nope, that only works for the window object. Commented Nov 10, 2011 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) Commented Nov 10, 2011 at 16:28
  • I could detect mousemove over the div but was hoping for a more comfortable solution. Commented Nov 10, 2011 at 16:30
  • For me overflow:hidden on body turned off resize event on FF and Chrome. Same issue? Commented Jun 6, 2023 at 19:48

6 Answers 6

33

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

const observer = new ResizeObserver(function(mutations) {
  console.log('mutations:', mutations);
});

const child = document.querySelector('textarea');
observer.observe(child);
<textarea></textarea>

Sign up to request clarification or add additional context in comments.

5 Comments

Indeed. Good idea!
Doesn't seem to work in Safari 10, even with WebKitMutationObserver.
Looks like this answer is outdated too, and you should be using ResizeObserver today instead.
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...
Note that attributes: true is not relevant to ResizeObserver(). Specs
12

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

8 Comments

Doesn't fire in Chrome, Linux
@Alp I believe DOM2 Mutation Events have been replaced by DOM4 Mutation Observers in recent builds. Try this article.
@robertc I dont think they work with resize code.google.com/p/chromium/issues/detail?id=293948
Thanks @robertc! The mentioned issue is now fixed and MutationObserver worked for me. I've added it as an answer.
@Boldewyn, somewhere between robertc's nice answer/comment and today, many years later, this has been deprecated: developer.mozilla.org/en-US/docs/Web/Guide/Events/… Care to accept one of the other answers for future readers?
|
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().

1 Comment

Yes, polling and mousemove are valid options, thanks. I thought of them myself. +1 for them. robertc's answer seems more elegant, though.
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+.

Comments

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 Felix's answer.

Especially useful when combining a custom search result panel with Dev Extreme's 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);
});

Comments

-2

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

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

2 Comments

I thought so, that onresize should fire in this case. I go looking, if there's a bug report over at bugzilla.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.