动态获取浏览器的宽度和高度

来源:互联网 发布:数据库攻击手段sql注入 编辑:程序博客网 时间:2024/05/17 03:43

常用的宽度属性  :

屏幕分辨率的宽:window.screen.width;

网页可见区域宽:document.body.clientWidth(不包括边框);

网页可见区域宽:document.body.offsetWidth(包括边的宽)

与之相对应的高度属性window.screen.height;document.body.height;document.body.offsetHeight.

利用这些属性,即可获取浏览器窗口的相关属性。

简单示例:

<head>
    <title></title>
    <meta http-equiv="content-type" content="text/html charset=utf-8">
</head>
<body>
    <input type="button" value="测试" id="btn1"
        style="width: 60px;border: 1px solid green;margin: 10px"/>
    <script>
        function func1(){
            var btn=document.getElementById("btn1");
            var  len=document.body.clientWidth;
            btn.value=len;
        }
        func1();
        window.onresize=func1;
    </script>
</body>

0 0