为了账号安全,请及时绑定邮箱和手机立即绑定

使用jquery检测元素溢出

使用jquery检测元素溢出

动漫人物 2019-09-02 17:15:58
CSS: .blue    {     width:200px;     height:200px;     background-color:blue;     color:#000000;     overflow:auto;    }JavaScript的:function addChar() {    $('.blue').append('some text  ');}HTML:<div id='blue1' class="blue"></div><br /><a href='javascript:void(0)' onclick='addChar()'>Add</a>div id='blue1'有overflow属性设置为auto。我想在发生溢出时检测溢出。
查看完整描述

3 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

$.fn.HasScrollBar = function() {

    //note: clientHeight= height of holder

    //scrollHeight= we have content till this height

    var _elm = $(this)[0];

    var _hasScrollBar = false; 

    if ((_elm.clientHeight < _elm.scrollHeight) || (_elm.clientWidth < _elm.scrollWidth)) {

        _hasScrollBar = true;

    }

    return _hasScrollBar;

}

///这是我的解决方案


查看完整回答
反对 回复 2019-09-02
?
手掌心

TA贡献1942条经验 获得超3个赞

针对具体问题的单线解决方案:


var elt, hasOverflow = (elt = $('#blue1')).innerWidth() > elt[0].scrollWidth;

只是想打高尔夫球。:]


(注意:这仅适用于水平溢出检测;添加垂直溢出检测也很简单。)


查看完整回答
反对 回复 2019-09-02
  • 3 回答
  • 0 关注
  • 500 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信