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

发布于 2024-08-27 21:14:42 字数 108 浏览 8 评论 0原文

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

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

Is there a simple way in jQuery to detect when scrollbars appear and disappear on a div that has overflow:auto? (Like an event? Fingers crossed...)

(I'd prefer not to have to look at the height of the content of the div)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼趣 2024-09-03 21:14:42

实现此目的的另一种方法是使用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);

Another way to achieve this is to check whether there are scrollbars present using scrollLeft or 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);
行至春深 2024-09-03 21:14:42

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

// 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;
}

在从 div 添加或删除内容后,您需要手动调用它,并且它可能仅在您在可见元素上调用它时才起作用,但这比从头开始要好。

As others have said, there is no easy way. Here's some code I've used in the past to detect if a scrollbar is present.

// 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;
}

You'll manually need to call this after adding or removing content from the div and it probably will only work if you call it on visible elements, but it's better than starting from scratch.

提笔书几行 2024-09-03 21:14:42

据我所知,没有相关事件。
但是,您“可以”为此编写自己的特殊事件,我想您必须检查一下
对于高度和宽度。

如果.innerHeight超过.outerHeight,应该可以检测到滚动条
一个元素的值。

As far as I know, there is not event for that.
However, you "could" write your own special event for that, I guess you have to check
for the height and width.

It should be possible to detect scrollbars if the .innerHeight exceds the .outerHeight
value of an element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文