JS获取浏览器可视区域的尺寸

一、

所谓可视区域是指能看得见的区域,即在浏览器中能看到页面的区域(高度与宽度)。刚刚使用 document.body.clientHeight 来获取可视区域的高度得到的却是整个文档的高度,然后在cnblogs.com的一篇文章中获知需要通过 document.documentElement.clientHeight 才能获取到浏览器的可视区域的高度,顺便将该文章摘下来,如下:

在没有声明DOCTYPE的IE中,浏览器显示窗口大小只能以下获取:

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; 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的浏览器中,可以用以下来获取浏览器显示窗口大小:

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 像素坐标。
 
二、用js实现分享到随页面滚动而滑动效果

页面向上向下滚动,分享到的模块随着滑动。

要点:

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);

获得页面垂直居中的位置

<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>无标题文档</title>
<style>
body{margin:0; padding:0; font:12px/1.5 arial; height:2001px;2000px;}
#share{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; 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>
 
原文地址:https://www.cnblogs.com/kissfu/p/6305939.html