js获取滚动条距离浏览器顶部,底部的高度

本文介绍了一种使用JavaScript获取滚动条距离浏览器顶部、底部高度的方法,包括获取窗口可视高度、滚动条高度及文档实际高度的函数实现,并通过实例演示了如何实时显示这些数据。
<html xmlns="http://www.phpernote.com/javascript-function/754.html">  

<head>  

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

<title>js获取滚动条距离浏览器顶部,底部的高度</title>  

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>  

<script type="text/javascript">  

//取窗口可视范围的高度  

function getClientHeight(){     

    var clientHeight=0;     

    if(document.body.clientHeight&&document.documentElement.clientHeight){     

        var clientHeight=(document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;             

    }else{     

        var clientHeight=(document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;         

    }     

    return clientHeight;     

}  

//取窗口滚动条高度  

function getScrollTop(){     

    var scrollTop=0;     

    if(document.documentElement&&document.documentElement.scrollTop){     

        scrollTop=document.documentElement.scrollTop;     

    }else if(document.body){     

        scrollTop=document.body.scrollTop;     

    }     

    return scrollTop;     

}  

//取文档内容实际高度  

function getScrollHeight(){     

    return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);     

}  

window.onscroll=function(){  

    var height=getClientHeight();  

    var theight=getScrollTop();  

    var rheight=getScrollHeight();  

    var bheight=rheight-theight-height;  

    document.getElementById('show').innerHTML='此时浏览器可见区域高度为:'+height+'<br />此时文档内容实际高度为:'+rheight+'<br />此时滚动条距离顶部的高度为:'+theight+'<br />此时滚动条距离底部的高度为:'+bheight;  

}  

function fixDiv(div_id,offsetTop){  

    var offsetTop=arguments[1]?arguments[1]:0;  

    var Obj=$('#'+div_id);  

    var ObjTop=Obj.offset().top;  

    var isIE6=$.browser.msie && $.browser.version == '6.0';  

    if(isIE6){  

        $(window).scroll(function(){  

            if($(window).scrollTop()<=ObjTop){  

                    Obj.css({  

                        'position':'relative',  

                        'top':0  

                    });  

            }else{  

                Obj.css({  

                    'position':'absolute',  

                    'top':$(window).scrollTop()+offsetTop+'px',  

                    'z-index':1  

                });  

            }  

        });  

    }else{  

        $(window).scroll(function(){  

            if($(window).scrollTop()<=ObjTop){  

                Obj.css({  

                    'position':'relative',  

                    'top':0  

                });  

            }else{  

                Obj.css({  

                    'position':'fixed',  

                    'top':0+offsetTop+'px',  

                    'z-index':1  

                });  

            }  

        });  

    }  

}  

$(function(){fixDiv('show',5);});  

</script>  

</head>  

<body>  

<div style="height:500px;"></div>  

<div>http://www.phpernote.com/jquery/251.html</div>  

<div id="show"></div>  

<div style="height:2000px;"></div>  

</body>  

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值