用jQuery检测滚动条的存在仍然很难吗? [英] Is detecting scrollbar presence with jQuery still difficult?

查看:159
本文介绍了用jQuery检测滚动条的存在仍然很难吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道检测滚动条的存在应该是我们都应该忍受的难以捉摸的事情之一。到目前为止我读到的是你无法真正检测到滚动条的存在,只能使用DOM中的提示来了解它们是否可能存在,而且不能在30以内完成代码行。

I know that detecting scrollbar presence is supposed to be one of those elusive things that we should all have to suffer through. What I've read so far is that you can't really detect scrollbar presence, only use hints in the DOM to know if they may be present, and it can't be done in less than 30 lines of code.

这对我来说听起来有点不可能现在我们在2010年.jQuery是否有一个跨浏览器可靠的解决方案来处理这个并至少在大多数情况下工作时间?请帮忙,我拉我的头发,一半已经在地板上了。

This sounds a little impossible to me now that we're in 2010. Does jQuery have a cross-browser reliable solution that takes care of this and works at least most of the time? Any help please, I'm pulling my hair, half of it is already on the floor.

推荐答案

可能不像你希望的那样优雅,但这是我最近编写的用于计算视口高度的脚本的充分适应。

Probably not as elegant as you were hoping for but this is an adequate adaption from a script I recently wrote to calculate viewport height.

从逻辑上讲,你想在文件就绪窗口调整大小时调用此函数

Logically you'd want to call this function on document ready and window resize.

它还处理您在Opera (第2行)和IE7 (第6行)。

It also deals with inconsistencies that you'd encounter in Opera (line 2) and IE7 (line 6).

function scrollbar() {
    var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();

    if (jQuery.browser.msie) {
        if(parseInt(jQuery.browser.version) == 7) {
            viewportHeight -= 3;
        }
    }

    if(viewportHeight <= $('#wrapper').height()) {
        return true;
    } else {
        return false;
    }
}

这篇关于用jQuery检测滚动条的存在仍然很难吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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