使用jQuery来检测容器溢出? [英] Use jQuery to Detect Container Overflow?

查看:89
本文介绍了使用jQuery来检测容器溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个问题,但觉得必须有一个更清洁的jQuery方法。我甚至不知道这是否真的在所有情况下都工作。有没有办法让jQuery确定一个容器是否溢出没有比较维?

I've seen this question but feel like there has to be a "cleaner" jQuery method of doing this. I'm not even sure if this really works in all scenarios. Is there a way for jQuery to determine if a container has overflow without comparing dimensions?

为了说明,是否有一个方法来测试CSS属性 overflow:hidden 已经踢入并正在隐藏内容?

For clarification, is there a method to test whether the CSS attribute overflow: hidden has kicked in and is hiding content?

推荐答案

$.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');
}

这篇关于使用jQuery来检测容器溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆