作web開發常常會碰到須要獲取瀏覽器的滾動條與頂部和底部的距離,而後作相應的處理動做。下面做者就如何經過js來獲取瀏覽器滾動條距離瀏覽器頂部和底部的高度作一下分享,這個是同時兼容ie和firefox的。javascript
獲取窗口可視範圍的高度php
- 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;
- }
獲取窗口滾動條高度css
- 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;
- }
獲取文檔內容實際高度html
- function getScrollHeight(){
- return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
- }
這裏是示例代碼效果圖:java
下面是具體的示例代碼,注意這裏爲了演示效果使用了固定懸浮框的效果,在ie下面固定懸浮效果與上面的代碼有些衝突不起做用,這裏不深究了,讀者能夠在firefox下面看效果,這個代碼自己是沒有問題的。jquery
- <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>