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

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

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

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

offsetHeig

10条回答
  •  猫巷女王i
    2020-12-01 12:19

    Use all the props, margin, border, padding and height in sequence.

    function getElmHeight(node) {
        const list = [
            'margin-top',
            'margin-bottom',
            'border-top',
            'border-bottom',
            'padding-top',
            'padding-bottom',
            'height'
        ]
    
        const style = window.getComputedStyle(node)
        return list
            .map(k => parseInt(style.getPropertyValue(k), 10))
            .reduce((prev, cur) => prev + cur)
    }
    

提交回复
热议问题