Determine if an HTML element's content overflows

前端 未结 6 974
误落风尘
误落风尘 2020-11-22 05:19

Can I use JavaScript to check (irrespective of scrollbars) if an HTML element has overflowed its content? For example, a long div with small, fixed size, the overflow proper

6条回答
  •  情话喂你
    2020-11-22 06:10

    Another way is compare the element width with its parent's width:

    function checkOverflow(elem) {
        const elemWidth = elem.getBoundingClientRect().width
        const parentWidth = elem.parentElement.getBoundingClientRect().width
    
        return elemWidth > parentWidth
    }
    

提交回复
热议问题