用于检测浏览器窗口的scrollTop的跨浏览器方法 [英] Cross-browser method for detecting the scrollTop of the browser window

查看:96
本文介绍了用于检测浏览器窗口的scrollTop的跨浏览器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的跨浏览器方式来检测浏览器窗口的scrollTop?我不喜欢使用任何预构建的代码库,因为这是一个非常简单的脚本,我不需要所有的重量。

What is the best cross-browser way to detect the scrollTop of the browser window? I prefer not to use any pre-built code libraries because this is a very simple script, and I don't need all of that deadweight.

推荐答案

function getScrollTop(){
    if(typeof pageYOffset!= 'undefined'){
        //most browsers except IE before #9
        return pageYOffset;
    }
    else{
        var B= document.body; //IE 'quirks'
        var D= document.documentElement; //IE with doctype
        D= (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}

alert(getScrollTop())

这篇关于用于检测浏览器窗口的scrollTop的跨浏览器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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