我看到了
this question,但是觉得这样做需要一个“更干净”的jQuery方法.我甚至不确定这是否真的适用于所有情况. jQuery是否有办法确定容器是否有溢出而不比较维度?
解决方法@H_403_6@
$.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;
};
例:
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');
}
$.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; };
例:
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'); }