jquery resize listener on a div

前端 未结 3 897
臣服心动
臣服心动 2020-11-30 06:47

This is the situation, I have 2 div\'s next to each other. One div is very dynamic in height, which basically means it can grow and shrink to accommodate for it\'s content.

相关标签:
3条回答
  • 2020-11-30 07:18

    As the thread poelinca provided suggests, there are some nice plugins available for this functionality.

    If you don't like the plugin idea, another simple solution would be to simply trigger a "resize" event on the div whenever the content is modified. Then you could monitor it with resize() as expected, utilizing an elegant observer pattern.

    function appendContent($div, content) {
       $div.append(content).trigger($.Event('resize'));
    }
    
    $div.bind('resize', function(e) {
       // all your magic resize mojo goes here
    });
    
    0 讨论(0)
  • 2020-11-30 07:23

    https://github.com/sdecima/javascript-detect-element-resize

    Works like a charm!

    I´ve tested attrchange but this was not working, lost 2hours of work :(

    0 讨论(0)
  • 2020-11-30 07:40

    The only thing I could think of is having some timer checking the height every X seconds.

    Something like this:

    function checkHeightTimer(){
        var div1 = $('#div1');
        var div2 = $('#div2');
        var somesize = #somenumber here;
        if(div1.height() > somesize){
            //do something to div2
        }
        setTimeout(checkHeightTimer, 500); //at 500 miliseconds
    }
    
    0 讨论(0)
提交回复
热议问题