首页 > 浏览器窗口大小变化时,如何获得当前文档显示区的宽高?

浏览器窗口大小变化时,如何获得当前文档显示区的宽高?

好像是用bom里的一些知识 记不太清了


window.onresize = function(){
    console.log(window.innerWidth);
    console.log(window.innerHeight);
}

你是说文档自适应窗口的大小么,用window.innerwidth window.innerHeight


说清楚如何变化?是缩小窗口,还是滑动滚动条?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    body {
        height: 2000px;
    }
    #div1{
        position:fixed;
        top:0;
        left: 0;
    }
</style>

<body>
    <div id="div1"></div>
</body>
<script>
    var temer;
    timer = setInterval(function () {
        var l = "文档的宽度:" + document.body.clientWidth + ", 文档的高度:" + document.body.clientHeight + ", 当前页面的宽度:" + document.documentElement.clientWidth + ", 当前页面的宽度:" + document.documentElement.clientHeight + ", 向下滑动的距离:" + document.body.scrollTop;

        document.getElementById("div1").innerHTML = l;
    }, 1000);
</script>

</html>
【热门文章】
【热门文章】