JS獲取瀏覽器可視區域的尺寸

1、javascript

所謂可視區域是指能看得見的區域,即在瀏覽器中能看到頁面的區域(高度與寬度)。剛剛使用 document.body.clientHeight 來獲取可視區域的高度獲得的倒是整個文檔的高度,而後在cnblogs.com的一篇文章中獲知須要經過 document.documentElement.clientHeight 才能獲取到瀏覽器的可視區域的高度,順便將該文章摘下來,以下:html

在沒有聲明DOCTYPE的IE中,瀏覽器顯示窗口大小隻能如下獲取:java

document.body.offsetWidth、document.body.clientWidth
document.body.offsetHeight、document.body.clientHeight
注意:若是body設置啦樣式會受到影響
body{margin:0; padding:0; font:12px/1.5 arial; height:2001px; width:2000px;}

//chrome測試,窗口手動改變大小,隨隨之改變的有:
//聲明瞭DOCTYPE
document.documentElement.clientHeight

document.documentElement.offsetWidth
document.documentElement.clientWidth

//沒有聲明瞭DOCTYPE
document.body.clientHeight
document.body.clientWidth
document.documentElement.clientWidth
document.documentElement.offsetWidth

 

        在聲明瞭DOCTYPE的瀏覽器中,能夠用如下來獲取瀏覽器顯示窗口大小:chrome

document.documentElement.clientWidth
document.documentElement.clientHeight

        IE,FF,Safari皆支持該方法,opera雖支持該屬性,可是返回的是頁面尺寸;瀏覽器

        同時,除了IE之外的全部瀏覽器都將此信息保存在window對象中,能夠用如下獲取:測試

window.innerWidth
window.innerHeight

網頁可見區域寬: document.body.clientWidth;
網頁可見區域高: document.body.clientHeight;
網頁可見區域高: document.body.offsetWidth; //包括邊線的寬
網頁可見區域高: document.body.offsetHeight; //包括邊線的寬
網頁正文全文寬: document.body.scrollWidth;
網頁正文全文高: document.body.scrollHeight;
網頁被捲去的高: document.body.scrollTop;
網頁被捲去的左: document.body.scrollLeft;
網頁正文部分上: window.screenTop;
網頁正文部分左: window.screenLeft;
屏幕分辨率的高: window.screen.height;
屏幕分辨率的寬: window.screen.width;
屏幕可用工做區高度: window.screen.availHeight;
屏幕可用工做區寬度: window.screen.availWidth;

clientX 設置或獲取鼠標指針位置相對於窗口客戶區域的 x 座標,其中客戶區域不包括窗口自身的控件和滾動條。
clientY 設置或獲取鼠標指針位置相對於窗口客戶區域的 y 座標,其中客戶區域不包括窗口自身的控件和滾動條。
offsetX 設置或獲取鼠標指針位置相對於觸發事件的對象的 x 座標。
offsetY 設置或獲取鼠標指針位置相對於觸發事件的對象的 y 座標。
screenX 設置或獲取獲取鼠標指針位置相對於用戶屏幕的 x 座標。
screenY 設置或獲取鼠標指針位置相對於用戶屏幕的 y 座標。
x 設置或獲取鼠標指針位置相對於父文檔的 x 像素座標。
y 設置或獲取鼠標指針位置相對於父文檔的 y 像素座標。
 
2、用js實現分享到隨頁面滾動而滑動效果

頁面向上向下滾動,分享到的模塊隨着滑動。spa

要點:指針

var scrtop =document.documentElement.scrollTop||document.body.scrollTop;
var height = document.documentElement.clientHeight||document.body.clientHeight;
var top = scrtop + (height - share.offsetHeight)/2;
top = parseInt(top);code

得到頁面垂直居中的位置htm

<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>無標題文檔</title>
<style>
body{margin:0; padding:0; font:12px/1.5 arial; height:2001px;width:2000px;}
#share{width:100px; height:200px; line-height:200px; text-align:center; border:1p solid #ccc; background:#f5f5f5; position:absolute; left:-100px; top:0;}
#share_tit{position:absolute; right:-20px; top:60px; width:20px; height:60px; padding:10px 0; background:#06c; text-align:center; line-height:18px; color:#fff;}
</style>
<script>
/*jingangel http://www.cnblogs.com/jingangel/ */
window.onload = function(){
  var share = document.getElementById("share");
    share.onmouseover = function(){
      startrun(share,0,"left")
    }
    share.onmouseout = function(){
      startrun(share,-100,"left")
    }
        window.onscroll = window.onresize = function(){
            var scrtop =document.documentElement.scrollTop||document.body.scrollTop;
            var height = document.documentElement.clientHeight||document.body.clientHeight;
            var top = scrtop + (height - share.offsetHeight)/2;
            top = parseInt(top);
            startrun(share,top,"top")
        }
}

var timer = null
function startrun(obj,target,direction){
    clearInterval(timer);
    timer = setInterval(function(){
      var speed = 0;
        
                if(direction == "left"){
                    speed = (target-obj.offsetLeft)/8;
                    speed = speed>0?Math.ceil(speed):Math.floor(speed);
                    
                    if(obj.offsetLeft == target){
                        clearInterval(timer);
                    }else{
                        obj.style.left = obj.offsetLeft + speed + "px";
                    }
                }
                
                 if(direction == "top"){
                    speed = (target-obj.offsetTop)/8;
                    speed = speed>0?Math.ceil(speed):Math.floor(speed);
                    
                    if(obj.offsetTop == target){
                        clearInterval(timer);
                    }else{
                        obj.style.top = obj.offsetTop + speed + "px";
                    }
                    document.title = obj.offsetTop + ',' + target + ',' +speed;
                }
        
    },30)
}
</script>
</head>

<body>

<div id="share">
  分享到內容
  <span id="share_tit">分享到</span>
</div>

</body>
</html>
相關文章
相關標籤/搜索