Is there an event that captures size changes of an HTML element?

6

I wonder if there is an event that captures size changes of an HTML element. For example, a div with 5000px height is 4800px and Javascript onresize() fault does not fire.

Is there an event that captures this?

HTML:

<div id="teste" style="height:1500px; background:#F00"></div>

JS:

// Evento que vai captura a mudança na altura dp documento
window.onresize = function(){
    alert('mudança na altura do documento');
}
// Diminui o div para disparar o evento
setTimeout(function(){ 
    document.getElementById('teste').style.height = '300px' 
}, 3000);

Fiddle

Note that the height of the document is changed but the resize event does not fire.

Thank you in advance.

    
asked by anonymous 09.12.2014 / 14:40

4 answers

1

A workaround:

var elemento = $("#teste");
var onResize = function() {
  elemento.css("background-color", "green");
  console.log("RESIZE!!");
};


elemento.data("size", {
  width: elemento[0].offsetWidth,
  height: elemento[0].offsetHeight
});

var interval = setInterval(function() {
  if (elemento.data("size").width != elemento[0].offsetWidth || elemento.data("size").height != elemento[0].offsetHeight) {
    onResize();
    clearInterval(interval);
  }
}, 100);



setTimeout(function() {
  elemento.css("width", "300px");
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="teste" style="width: 200px;height:200px;background-color: red;">
  <div>
    
09.02.2015 / 21:07
0

[UPDATED] JavaScript:

$('#test_div').bind('resize', function(){
            console.log('resized');
});

$(window).resize(function(){
   $('#test_div').resize();
});

Example: link

    
09.12.2014 / 15:17
0

Hello,

You can also find out when you resize with CSS3.

@media only screen and (min-width: 4800px) and (max-width: 5000px) { 
  background-color:red;
}

@media only screen and (max-width: 4800px) { background-color:blue; }
    
09.12.2014 / 15:32
0

You can take a look at link

It triggers the resize event, however it is likely to fire only using the handlers it provides.

An example:

$( "#teste" ).resizable({ 
    resize: function(e,ui) { 
        console.log("deu resize!") 
    } 
});
    
09.02.2015 / 19:29