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

做web开发经常会碰到需要获取浏览器的滚动条与顶部和底部的距离,然后做相应的处理动作。下面作者就如何通过js来获取浏览器滚动条距离浏览器顶部和底部的高度做一下分享,这个是同时兼容ie和firefox的。

首先我们需要知道的两个定义是:

滚动条距离浏览器顶部的高度 = 窗口滚动条高度;

滚动条距离浏览器底部的高度 = 文档(页面)内容实际高度 - 滚动条距离浏览器顶部的高度 - 窗口可视范围的高度;

好了,明白了这个定义,那我们就来具体看看如何获取各种高度的方法,下面同时举了一个示例。

获取窗口可视范围的高度

 
 
  1. function getClientHeight(){    
  2.     var clientHeight=0;    
  3.     if(document.body.clientHeight&&document.documentElement.clientHeight){    
  4.         var clientHeight=(document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;            
  5.     }else{    
  6.         var clientHeight=(document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;        
  7.     }    
  8.     return clientHeight;    
  9. } 

获取窗口滚动条高度

 
 
  1. function getScrollTop(){    
  2.     var scrollTop=0;    
  3.     if(document.documentElement&&document.documentElement.scrollTop){    
  4.         scrollTop=document.documentElement.scrollTop;    
  5.     }else if(document.body){    
  6.         scrollTop=document.body.scrollTop;    
  7.     }    
  8.     return scrollTop;    
  9. } 

获取文档内容实际高度

 
 
  1. function getScrollHeight(){    
  2.     return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);    
  3. } 

下面是具体的示例代码,注意这里为了演示效果使用了固定悬浮框的效果,在ie下面固定悬浮效果与上面的代码有些冲突不起作用,这里不深究了,读者可以在firefox下面看效果,这个代码本身是没有问题的。

 
 
  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4. <title>js获取滚动条距离浏览器顶部,底部的高度</title> 
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
  6. <script type="text/javascript"> 
  7. //取窗口可视范围的高度 
  8. function getClientHeight(){    
  9.     var clientHeight=0;    
  10.     if(document.body.clientHeight&&document.documentElement.clientHeight){    
  11.         var clientHeight=(document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;            
  12.     }else{    
  13.         var clientHeight=(document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;        
  14.     }    
  15.     return clientHeight;    
  16. } 
  17. //取窗口滚动条高度 
  18. function getScrollTop(){    
  19.     var scrollTop=0;    
  20.     if(document.documentElement&&document.documentElement.scrollTop){    
  21.         scrollTop=document.documentElement.scrollTop;    
  22.     }else if(document.body){    
  23.         scrollTop=document.body.scrollTop;    
  24.     }    
  25.     return scrollTop;    
  26. } 
  27. //取文档内容实际高度 
  28. function getScrollHeight(){    
  29.     return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);    
  30. } 
  31. window.onscroll=function(){ 
  32.     var height=getClientHeight(); 
  33.     var theight=getScrollTop(); 
  34.     var rheight=getScrollHeight(); 
  35.     var bheight=rheight-theight-height; 
  36.     document.getElementById('show').innerHTML='此时浏览器可见区域高度为:'+height+'<br />此时文档内容实际高度为:'+rheight+'<br />此时滚动条距离顶部的高度为:'+theight+'<br />此时滚动条距离底部的高度为:'+bheight; 
  37. } 
  38. function fixDiv(div_id,offsetTop){ 
  39.     var offsetTop=arguments[1]?arguments[1]:0; 
  40.     var Obj=$('#'+div_id); 
  41.     var ObjObjTop=Obj.offset().top; 
  42.     var isIE6=$.browser.msie && $.browser.version == '6.0'; 
  43.     if(isIE6){ 
  44.         $(window).scroll(function(){ 
  45.             if($(window).scrollTop()<=ObjTop){ 
  46.                     Obj.css({ 
  47.                         'position':'relative', 
  48.                         'top':0 
  49.                     }); 
  50.             }else{ 
  51.                 Obj.css({ 
  52.                     'position':'absolute', 
  53.                     'top':$(window).scrollTop()+offsetTop+'px', 
  54.                     'z-index':1 
  55.                 }); 
  56.             } 
  57.         }); 
  58.     }else{ 
  59.         $(window).scroll(function(){ 
  60.             if($(window).scrollTop()<=ObjTop){ 
  61.                 Obj.css({ 
  62.                     'position':'relative', 
  63.                     'top':0 
  64.                 }); 
  65.             }else{ 
  66.                 Obj.css({ 
  67.                     'position':'fixed', 
  68.                     'top':0+offsetTop+'px', 
  69.                     'z-index':1 
  70.                 }); 
  71.             } 
  72.         }); 
  73.     } 
  74. } 
  75. $(function(){fixDiv('show',5);}); 
  76. </script> 
  77. </head> 
  78. <body> 
  79. <div style="height:500px;"></div> 
  80. <div>http://www.daimajiayuan.com/sitejs-17231-1.html</div> 
  81. <div id="show"></div> 
  82. <div style="height:2000px;"></div> 
  83. </body> 
  84. </html> 


源引:http://www.daimajiayuan.com/sitejs-17250-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值