简体   繁体   中英

Is there a JavaScript event to detect when the size changes for a div using the CSS3 resize property?

I'm using the CSS3 resize property to create a vertically scrollable div:

.mydiv {
  resize: vertical
  overflow: auto;
}

Chrome puts a little set of diagonal hatches in the bottom right corner of the div which I can now use to resize it.

Is there any way to detect the resize in JavaScript? I've tried using a DOM4 Mutation Observer on the div's attributes but it doesn't fire anything. I can use mouse events on the div but it's hacky (mousedown doesn't fire so I have to listen to all mousemove's and detect if it's near that corner.)

It doesn't appear possible to attach an event to the resize using the Chrome textarea handles. Read this: jQuery : Chrome textareas and resize event

I tried using the DOMSubtreeModified but it doesn't modify the DOM so that doesn't work.

--UPDATE-- If you want to use jQuery UI then it is possible. See this: http://jsfiddle.net/kJNsB/1/

$("textarea").resizable({
    resize: function() {
        $("body").append("<pre>resized! Height:"+$(this).height()+" /// Width:"+$(this).width()+"</pre>");
    }
});

--UPDATE-- This guy has it figured out! http://benalman.com/code/projects/jquery-resize/examples/resize/

Can you try this,

HTML:

<textarea onresize="Resizing()"></textarea>

Javascript:

function Resizing(){
   console.log("Resizing..");
}

Right now only window object can have a resize event handler. But you can use getBoundingClientRect() on an element.

https://developer.mozilla.org/en-US/docs/Web/Events/resize

In some earlier browsers it was possible to register resize event handlers on any HTML element. It is still possible to set onresize attributes or use addEventListener() to set a handler on any element. However, resize events are only fired on the window object (ie returned by document.defaultView). Only handlers registered on the window object will receive resize events.

There is a proposal to allow all elements to be notified of resize changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM