Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a function everytime a div changes its size?

How to run a function everytime a div changes its size?

Div's size normally changes when the window changes its size, but NOT only; The new width/height are not everytime set by css, but I need to get the real width/height of that height when everytime it changes.

I use jQuery, so based on this would be good;

It must work in chrome, ff2, ff3, ie6(if can't work I might lag user's browser with a timer xD) ie7, safari, opera... in other words the most popular browsers

thanks ;)

like image 282
Totty.js Avatar asked Jan 27 '11 00:01

Totty.js


People also ask

How do I change the size of a div dynamically?

The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement.

How do I make div width auto adjust?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

How auto adjust the div height according to content?

Syntax: height: length|percentage|auto|initial|inherit; Property Values: height: auto; It is used to set height property to its default value.

How do you call a function inside a div tag?

You can name each function according to the div 's id and call it dynamically using the object["methodName"]() syntax to call it.


3 Answers

This question has been asked '11, but as I came here via Google I want to clarify some things.

1) There is already a solid method to check if a element has been changed in almost all browsers. Check for this my lib:

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

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

2) Please do not use the jQuery onresize plugin as it uses setTimeout() loop to check for changes. THIS IS INCREDIBLY SLOW AND NOT ACCURATE.

like image 137
Marc J. Schmidt Avatar answered Oct 09 '22 14:10

Marc J. Schmidt


Here is a plugin that extends jQuery's resize() event to work on any element, not just the window.

like image 25
keithjgrant Avatar answered Oct 09 '22 13:10

keithjgrant


Try the onresize event (http://www.w3schools.com/jsref/event_onresize.asp):

<div onresize="alert('You have changed the size of the window')"></div>
like image 27
JK. Avatar answered Oct 09 '22 14:10

JK.