Full height of a html element (div) including border, padding and margin?

后端 未结 10 1729
悲&欢浪女
悲&欢浪女 2020-12-01 11:42

I need the full height of a div, I\'m currently using

document.getElementById(\'measureTool\').offsetHeight

offsetHeig

10条回答
  •  情歌与酒
    2020-12-01 12:04

    If you can use jQuery:

    $('#divId').outerHeight(true) // gives with margins.
    

    jQuery docs

    For vanilla javascript you need to write a lot more (like always...):

    function Dimension(elmID) {
        var elmHeight, elmMargin, elm = document.getElementById(elmID);
        if(document.all) {// IE
            elmHeight = elm.currentStyle.height;
            elmMargin = parseInt(elm.currentStyle.marginTop, 10) + parseInt(elm.currentStyle.marginBottom, 10) + "px";
        } else {// Mozilla
            elmHeight = document.defaultView.getComputedStyle(elm, '').getPropertyValue('height');
            elmMargin = parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-top')) + parseInt(document.defaultView.getComputedStyle(elm, '').getPropertyValue('margin-bottom')) + "px";
        }
        return (elmHeight+elmMargin);
    }
    

    ​ Live DEMO

    code source

提交回复
热议问题