javascript - 溢出检查不起作用

标签 javascript html css

https://jsfiddle.net/basickarl/apts370w/1/

在这种情况下,我希望 checkOverflow 函数返回 true

HTML:

<div style="float:right;">
  <div id="z">
    <div id="txt">
      345345345345345345345345345345345
    </div>
  </div>
</div>

Javascript:

console.log(checkOverflow(document.getElementById('z')));

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;
};

CSS:

div#z {
  position: relative;
  background: red;
  width: 50px;
  height: 50px;
  overflow: auto;
}

div#txt {
  background: lightblue;
  float: right;
}

知道为什么函数的 console.log 在这里返回 true 吗?

最佳答案

scrollHeightscrollWidth分别对应实际的clientHeightclientWdith+隐藏的高度或宽度。由于 div#txt 向右浮动,因此不会溢出。

你应该看看 box modelthis answer

关于javascript - 溢出检查不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35397573/

相关文章:

javascript - React-Native map 自定义CSS或样式

javascript - Json数据提取

javascript - 用 2 个背景图像制作翻转拼图

jquery - 如何在 ajax 响应的结果中删除 div 中的 css 类

html - CSS HTML 当我以像素为单位指定宽度时,为什么会根据屏幕宽度调整大小?

javascript - 使用 $.ajax() 过滤 XML 文件

javascript - Googlebot 和 Javascript 中的 URL

html - 为什么我的轮播不能正常工作?

CSS 元素定位

html - 我需要帮助找到该函数的名称