我见过这个问题 https://stackoverflow.com/questions/2059743/detect-elements-overflow-using-jquery但感觉必须有一个“更干净”的 jQuery 方法来做到这一点。我什至不确定这是否真的适用于所有场景。 jQuery 有没有办法在不比较尺寸的情况下确定容器是否溢出?
为了澄清一下,有没有一种方法可以测试 CSS 属性是否overflow: hidden
已启动并正在隐藏内容?
$.fn.hasOverflow = function() {
var $this = $(this);
var $children = $this.find('*');
var len = $children.length;
if (len) {
var maxWidth = 0;
var maxHeight = 0
$children.map(function(){
maxWidth = Math.max(maxWidth, $(this).outerWidth(true));
maxHeight = Math.max(maxHeight, $(this).outerHeight(true));
});
return maxWidth > $this.width() || maxHeight > $this.height();
}
return false;
};
Example:
var $content = $('#content').children().wrapAll('<div>');
while($content.hasOverflow()){
var size = parseFloat($content.css('font-size'), 10);
size -= 1;
$content.css('font-size', size + 'px');
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)