Find the “potential” width of a hidden element

前端 未结 10 1335
慢半拍i
慢半拍i 2020-12-03 09:31

I\'m currently extending the lavalamp plugin to work on dropdown menus but I\'ve encountered a small problem. I need to know the offsetWidth of an element that

10条回答
  •  再見小時候
    2020-12-03 10:27

    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

提交回复
热议问题