javascript css检查溢出 [英] javascript css check if overflow

查看:100
本文介绍了javascript css检查溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检测一个元素是否溢出是什么的简单方法?

What's the easist way to detect if an element has been overflowed?

我的用例是,我想限制某个内容框的高度为300px 。如果内容比这高,我用溢出切掉它。但是如果它溢出,我想显示一个更多按钮,但如果不是我不想显示该按钮。

My use case is, I want to limit a certain content box to have a height of 300px. If the inner content is taller than that, I cut it off with an overflow. But if it is overflowed I want to show a 'more' button, but if not I don't want to show that button.

有一个简单的方法来检测溢出,还是有更好的方法?谢谢。

Is there an easy way to detect overflow, or is there a better method? Thanks.

推荐答案

如果DOM元素溢出,此函数将返回布尔值:

This function will return you boolean value if the DOM element is overflowed:

function isOverflowed(element){
    return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
}

元素可能会垂直或水平溢出或两者都溢出

The element may be overflowed vertically or horizontally or both

这篇关于javascript css检查溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆