您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

JavaScript确定HTML元素的内容是否溢出

JavaScript确定HTML元素的内容是否溢出

通常,您可以比较client[Height|Width]scroll[Height|Width]以便检测到此情况… …但是,当可见溢出时,这些值将相同。因此,检测程序必须考虑到这一点:

// Determines if the passed element is overflowing its bounds,
// either vertically or horizontally.
// Will temporarily modify the "overflow" style to detect this
// if necessary.
function checkOverflow(el)
{
   var curOverflow = el.style.overflow;

   if ( !curOverflow || curOverflow === "visible" )
      el.style.overflow = "hidden";

   var isOverflowing = el.clientWidth < el.scrollWidth 
      || el.clientHeight < el.scrollHeight;

   el.style.overflow = curOverflow;

   return isOverflowing;
}

在FF3,FF40.0.2,IE6,Chrome 0.2.149.30中进行了测试。

javascript 2022/1/1 18:13:45 有569人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶