溢出检查不起作用
Overflow check not working
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
吗?
scrollHeight
和 scrollWidth
对应于实际clientHeight
和clientWdith
+ 隐藏高度或宽度。由于div#txt
向右浮动,因此不会溢出。
你应该看看盒子模型和这个答案。
问题是#txt
向右浮动,因此向左溢出。
但是使用默认的从左到右方向时,无法向左滚动。
但是,如果您使用从右到左的方向,则可以
#z { direction: rtl; }
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;
}
console.log(checkOverflow(document.getElementById('z')));
div#z {
position: relative;
background: red;
width: 50px;
height: 50px;
overflow: auto;
direction: rtl;
}
div#txt {
background: lightblue;
float: right;
}
<div style="float:right;">
<div id="z">
<div id="txt">
345345345345345345345345345345345
</div>
</div>
</div>
当然,如果内容向右溢出(例如,您更改为float: left
(,您将遇到同样的问题。
如果你想要一个更通用的方法,你可以使用getBoundingClientRect
:
var rect = el.getBoundingClientRect();
var isOverflowing = [].every.call(el.children, function(child) {
var childRect = child.getBoundingClientRect();
return childRect.left < rect.left
|| childRect.right > rect.right
|| childRect.top < rect.top
|| childRect.bottom > rect.bottom;
});
function checkOverflow(el) {
var curOverflow = getComputedStyle(el).overflow;
if (!curOverflow || curOverflow === 'visible')
el.style.overflow = 'hidden';
var rect = el.getBoundingClientRect();
var isOverflowing = [].every.call(el.children, function(child) {
var childRect = child.getBoundingClientRect();
return childRect.left < rect.left
|| childRect.right > rect.right
|| childRect.top < rect.top
|| childRect.bottom > rect.bottom;
});
el.style.overflow = curOverflow;
return isOverflowing;
}
console.log(checkOverflow(document.getElementById('z')));
div#z {
position: relative;
background: red;
width: 50px;
height: 50px;
overflow: auto;
}
div#txt {
background: lightblue;
float: right;
}
<div style="float:right;">
<div id="z">
<div id="txt">
345345345345345345345345345345345
</div>
</div>
</div>
这是我
的解决方案
https://jsfiddle.net/apts370w/3/
问题是它不是内联元素,如果您更改为 span 标签,它可以工作
我也改变这个
el.offsetWidth //instead of clientWidth
相关文章:
- 使用map来检查是否为真'不起作用
- 错误检查在Javascript中不起作用
- 对照数组脚本检查文本框中的值不起作用
- JS中的单选按钮检查不起作用
- 为什么这个jQuery代码检查所有复选框不起作用
- 简单的PHP字符串检查不起作用
- 引导程序3在IE8中不起作用(已检查其他答案)
- 我需要让用户输入他的出生日期,并使用javascript检查其形式是否正确.我的代码不起作用
- 我的数字检查器功能不起作用,为什么
- If.else语句检查NaN和空字符串是否在JavaScript中不起作用
- javascript对象检查在IE11中不起作用
- 在移动设备领域,空白:nowrap;溢出:隐藏;文本溢出:省略号;有点不起作用
- 检查window.location.href.indexOf是否'不起作用
- Iframe滚动溢出隐藏在css中不起作用
- 检查变量是否未定义不起作用
- PHP/JavaScript 检查/取消检查在 FF 中工作,但在 IE 中不起作用
- 类绑定在 emberjs 中不起作用,因为不检查属性
- 设置 $(element).style.backgroundColor,然后检查但不起作用
- 检查未定义的变量在 chrome 中不起作用
- 溢出检查不起作用