滚动条在jQuery中出现/消失事件?

Tra*_*vis 16 javascript jquery scroll scrollbar javascript-events

有没有一种简单的方法在jQuery中检测滚动条何时出现并消失在有溢出的div上:auto?(就像一个事件?手指交叉......)

(我不想看看div内容的高度)

not*_*ans 7

另一种实现此目的的方法是使用scrollLeft或scrollTop检查是否存在滚动条:

//nudge the scrollbar away from its starting position

$('#your_selector').scrollLeft(1);

//A value of 0 is assigned if the scrollbars are at their default position, 
//or are abscent

if($('#your_selector').scrollLeft() !== 0) return true;

//put the scrollbar back to its starting position

$('#your_selector').scrollLeft(0);
Run Code Online (Sandbox Code Playgroud)


tim*_*fin 6

正如其他人所说,没有简单的方法.这是我过去用来检测滚动条是否存在的一些代码.

// Used like $('#my-id').hasScrollbar();

jQuery.fn.hasScrollbar = function() {
    var scrollHeight = this.get(0).scrollHeight;

    //safari's scrollHeight includes padding
    if ($.browser.safari)
        scrollHeight -= parseInt(this.css('padding-top')) + parseInt(this.css('padding-bottom'));

    if (this.height() < scrollHeight)
        return true;
    else
        return false;
}
Run Code Online (Sandbox Code Playgroud)

在div中添加或删除内容后,您手动需要调用它,它可能仅在您在可见元素上调用它时才有效,但它比从头开始更好.