文档宽高及窗口事件

获取div的可视区域的大小 clientWidth clientHeight

var odiv=document.getElementById('odiv');
alert(odiv.clientWidth);

获取整个窗口的大小

alert(document.documentElement.clientWidth);
alert(document.documentElement.clientHeight);

滚动距离 scrollTop scrollLeft

   document.onclick= function () {
       //滚动条滚动的距离  document.documentElement.scrollTop[scrollLeft]
       //谷歌认为滚动条是属于body里面的
    //  alert(document.documentElement.scrollTop);
    //  alert(document.body.scrollTop);
      var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
   }

内容高度 scrollWidth scrollHeight

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        #con{
            width: 100px;
            height: 100px;
            border: 1px solid black;
            padding: 10px;
        }
        #son{
            width: 100px;
            height: 300px;
            background: pink;
        }
    </style>
</head>
<body>
<div id="con">
    <div id="son"></div>
</div>
<script>
   document.onclick= function () {
       var con=document.getElementById('con');
       alert(con.scrollWidth);
   }
</script>
</body>
</html>

文档高度 offsetHeight

<div style="height: 200px;margin: 20px;width: 100px;background: pink"></div>
alert(document.documentElement.offsetHeight);
alert(document.body.offsetHeight);

onscroll:当滚动条滚动的时候触发
onresize:当窗口大小发生改变的时候触发

<body style="height: 2000px">
<script>
    var i=0;
//i是根据时间间隔来计算的
    window.onscroll= function () {
          document.title=i++;
    };
</script>
</body>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容