自动获取屏幕尺寸信息的html文件

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动获取屏幕尺寸信息的html文件</title>
    <style>
        #testDiv {
            width: 100%;
            height: 100vh; /* 100% of the viewport height */
            background-color: lightblue;
            text-align: center;
            font-size: 2em;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    </style>
</head>
<body>
    <div id="testDiv">
        宽度: <span id="width"></span>px, 高度: <span id="height"></span>px
    </div>

    <script>
        function updateSize() {
            const div = document.getElementById('testDiv');
            const width = div.offsetWidth;
            const height = div.offsetHeight;
            document.getElementById('width').textContent = width;
            document.getElementById('height').textContent = height;
        }

        window.onload = updateSize;
        window.onresize = updateSize;
    </script>
</body>
</html>
相关推荐
谦行几秒前
深度神经网络训练过程与常见概念
前端
Simon_He30 分钟前
一个免费的在线压缩网站超越了付费的压缩软件
前端·开源·图片资源
巴巴_羊1 小时前
React Ref使用
前端·javascript·react.js
拾光拾趣录1 小时前
CSS常见问题深度解析与解决方案(第三波)
前端·css
徊忆羽菲2 小时前
Echarts3D柱状图-圆柱体-文字在柱体上垂直显示的实现方法
javascript·ecmascript·echarts
轻语呢喃2 小时前
JavaScript :字符串模板——优雅编程的基石
前端·javascript·后端
杨进军2 小时前
React 协调器 render 阶段
前端·react.js·前端框架
中微子2 小时前
Blob 对象及 Base64 转换指南
前端
风铃喵游2 小时前
让大模型调用MCP服务变得超级简单
前端·人工智能
中微子2 小时前
智能前端实践之 shot-word demo
前端