Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onresize for div elements?

Visual Studio highlighted my onresize attribute of my div tag, and says that it isn't a valid attribute for HTML5. Is this true? What should I use instead? It seems kind of silly that this would be the case.

like image 719
Oztaco Avatar asked Oct 12 '13 01:10

Oztaco


People also ask

How do you check if a div is resized?

In the mean time, you can use function like the following. Since, the majority of element size changes will come from the window resizing or from changing something in the DOM. You can listen to window resizing with the window's resize event and you can listen to DOM changes using MutationObserver .

What is Onresize HTML?

Definition and Usage. The onresize event occurs when the browser window has been resized. Tip: To get the size of an element, use the clientWidth, clientHeight, innerWidth, innerHeight, outerWidth, outerHeight, offsetWidth and/or offsetHeight properties.

How does jQuery determine Div resize?

The height and width of the element to be tracked is found out using the height() and width() method in jQuery.


2 Answers

Only Window.onResize exists in the specification. Please remember that every IFrame element creates new Window object which supports onResize. Therefore IMO the most reliable method to detect changes to the element's size is to append hidden iframes to the element.

If you are interested in a neat and portable solution, please check this plugin. It takes 1 line of code to listen the event of changing width or height of your div.

<!-- (1) include plugin script in a page --> <script src="/src/jquery-element-onresize.js"></script>  // (2) use the detectResizing plugin to monitor changes to the element's size: $monitoredElement.detectResizing({ onResize: monitoredElement_onResize });  // (3) write a function to react on changes: function monitoredElement_onResize() {         // logic here... } 
like image 40
rbtbar Avatar answered Oct 03 '22 23:10

rbtbar


Microsoft's Internet Explorer supports onresize on all HTML elements. In all other Browsers the onresize is only available at the window object. https://developer.mozilla.org/en-US/docs/Web/API/Window.onresize

If you wanna have onresize at a div in all browsers check this:

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

This library has a class ResizeSensor which can be used for resize detection.

like image 180
Marc J. Schmidt Avatar answered Oct 03 '22 23:10

Marc J. Schmidt