jQuery: Get height of hidden element in jQuery

后端 未结 14 2218
南方客
南方客 2020-11-22 09:49

I need to get height of an element that is within a div that is hidden. Right now I show the div, get the height, and hide the parent div. This seems a bit silly. Is there a

14条回答
  •  伪装坚强ぢ
    2020-11-22 10:30

    I try to find working function for hidden element but I realize that CSS is much complex than everyone think. There are a lot of new layout techniques in CSS3 that might not work for all previous answers like flexible box, grid, column or even element inside complex parent element.

    flexibox example enter image description here

    I think the only sustainable & simple solution is real-time rendering. At that time, browser should give you that correct element size.

    Sadly, JavaScript does not provide any direct event to notify when element is showed or hidden. However, I create some function based on DOM Attribute Modified API that will execute callback function when visibility of element is changed.

    $('[selector]').onVisibleChanged(function(e, isVisible)
    {
        var realWidth = $('[selector]').width();
        var realHeight = $('[selector]').height();
    
        // render or adjust something
    });
    

    For more information, Please visit at my project GitHub.

    https://github.com/Soul-Master/visible.event.js

    demo: http://jsbin.com/ETiGIre/7

提交回复
热议问题