自动获取屏幕尺寸信息的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>
相关推荐
小小小小宇3 分钟前
前端监测用户卡顿之INP
前端
小小小小宇9 分钟前
监测用户在浏览界面过程中的卡顿
前端
糖墨夕10 分钟前
Nest 是隐藏的“设计模式大佬”
前端
逾明1 小时前
Electron自定义菜单栏及Mac最大化无效的问题解决
前端·electron
辰九九1 小时前
Uncaught URIError: URI malformed 报错如何解决?
前端·javascript·浏览器
月亮慢慢圆1 小时前
Echarts的基本使用(待更新)
前端
芜青2 小时前
实现文字在块元素中水平/垂直居中详解
前端·css·css3
useCallback2 小时前
Elpis全栈项目总结
前端
小高0072 小时前
React useMemo 深度指南:原理、误区、实战与 2025 最佳实践
前端·javascript·react.js
LuckySusu2 小时前
【js篇】深入理解类数组对象及其转换为数组的多种方法
前端·javascript