首页 > 其他分享 >了解 HTML 中的window、document、body和元素的尺寸及位置

了解 HTML 中的window、document、body和元素的尺寸及位置

时间:2024-05-27 10:32:40浏览次数:18  
标签:body console log screen width window HTML document

本文将详细介绍与窗口(window)、文档(document)、主体(body)以及其他 HTML 元素的尺寸和位置相关的属性及方法。每个部分将包含示例和代码注释,帮助您更好地理解和应用这些知识。

window 对象尺寸属性

1. window.innerWidthwindow.innerHeight

window.innerWidthwindow.innerHeight 分别表示浏览器视口(viewport)的宽度和高度。视口是用户实际看到的网页部分,不包括浏览器的工具栏、菜单栏等。

<!DOCTYPE html>
<html>
<head>
  <title>Window Size Example</title>
</head>
<body>
  <script>
    console.log("Viewport width: " + window.innerWidth);
    console.log("Viewport height: " + window.innerHeight);
  </script>
</body>
</html>
2. window.outerWidthwindow.outerHeight

window.outerWidthwindow.outerHeight 分别表示浏览器窗口的整体宽度和高度,包括窗口的边框、工具栏等。

<!DOCTYPE html>
<html>
<head>
  <title>Window Outer Size Example</title>
</head>
<body>
  <script>
    console.log("Window outer width: " + window.outerWidth);
    console.log("Window outer height: " + window.outerHeight);
  </script>
</body>
</html>

screen 对象尺寸属性

1. screen.widthscreen.height

screen.widthscreen.height 分别表示用户屏幕的宽度和高度。这些属性反映的是屏幕的物理分辨率。

<!DOCTYPE html>
<html>
<head>
  <title>Screen Size Example</title>
</head>
<body>
  <script>
    console.log("Screen width: " + screen.width);
    console.log("Screen height: " + screen.height);
  </script>
</body>
</html>
2. screen.availWidthscreen.availHeight

screen.availWidthscreen.availHeight 表示用户屏幕可用的宽度和高度,通常排除了操作系统的任务栏等不可用区域。

<!DOCTYPE html>
<html>
<head>
  <title>Available Screen Size Example</title>
</head>
<body>
  <script>
    console.log("Available screen width: " + screen.availWidth);
    console.log("Available screen height: " + screen.availHeight);
  </script>
</body>
</html>
3. screenTopscreenLeft

screenTopscreenLeft 表示当前窗口相对于屏幕左上角的水平和垂直偏移。这些属性主要用于多屏幕环境下的窗口定位。

4. 兼容性优势

screen 对象的这些属性在所有浏览器中均有良好的兼容性,无需担心浏览器差异。

元素尺寸属性

1. clientWidthclientHeight

clientWidthclientHeight 表示元素的内部宽度和高度,不包括边框和滚动条,但包括内边距(padding)。

<!DOCTYPE html>
<html>
<head>
  <title>Client Size Example</title>
</head>
<body>
  <div id="myElement" style="width: 200px; height: 100px; padding: 20px; border: 5px solid;">
    Example Element
  </div>
  <script>
    var element = document.getElementById("myElement");
    console.log("Client width: " + element.clientWidth);
    console.log("Client height: " + element.clientHeight);
  </script>
</body>
</html>
2. offsetWidthoffsetHeight

offsetWidthoffsetHeight 表示元素的完整尺寸,包括边框、内边距和滚动条,但不包括外边距(margin)。

<!DOCTYPE html>
<html>
<head>
  <title>Offset Size Example</title>
</head>
<body>
  <div id="myElement" style="width: 200px; height: 100px; padding: 20px; border: 5px solid;">
    Example Element
  </div>
  <script>
    var element = document.getElementById("myElement");
    console.log("Offset width: " + element.offsetWidth);
    console.log("Offset height: " + element.offsetHeight);
  </script>
</body>
</html>
3. offsetParentoffsetTopoffsetLeft

这些属性用于获取元素相对于其最近的定位祖先元素的位置。

<!DOCTYPE html>
<html>
<head>
  <title>Offset Position Example</title>
</head>
<body>
  <div id="parentElement" style="position: relative;">
    <div id="childElement" style="position: absolute; top: 50px; left: 30px;">
      Child Element
    </div>
  </div>
  <script>
    var child = document.getElementById("childElement");
    console.log("Offset parent: " + child.offsetParent.id);
    console.log("Offset top: " + child.offsetTop);
    console.log("Offset left: " + child.offsetLeft);
  </script>
</body>
</html>

特殊元素 documentElementbody

1. 介绍 documentElementbody 的区别及其尺寸属性

documentElement 通常指的是 <html> 元素,而 body 指的是 <body> 元素。二者的尺寸属性有些许差异。

<!DOCTYPE html>
<html>
<head>
  <title>Document Element Example</title>
</head>
<body>
  <div style="height: 1500px;">
    Long Content
  </div>
  <script>
    console.log("Document width: " + document.documentElement.clientWidth);
    console.log("Document height: " + document.documentElement.clientHeight);
    console.log("Body width: " + document.body.clientWidth);
    console.log("Body height: " + document.body.clientHeight);
  </script>
</body>
</html>
2. 如何根据内容动态调整尺寸

在页面加载或内容改变时,可以通过 JavaScript 动态调整元素的尺寸以适应新的内容。

<!DOCTYPE html>
<html>
<head>
  <title>Dynamic Size Adjustment Example</title>
</head>
<body>
  <div id="dynamicElement" style="width: 100px; height: 100px; border: 1px solid;">
    Resize me!
  </div>
  <button onclick="adjustSize()">Adjust Size</button>
  <script>
    function adjustSize() {
      var element = document.getElementById("dynamicElement");
      element.style.width = "300px";
      element.style.height = "300px";
    }
  </script>
</body>
</html>

获取视口大小的兼容性写法

1. 展示如何使用条件运算符获取视口尺寸

为了确保在所有浏览器中兼容,可以使用条件运算符来获取视口的宽高。

<!DOCTYPE html>
<html>
<head>
  <title>Viewport Size Compatibility Example</title>
</head>
<body>
  <script>
    var viewportWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var viewportHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    console.log("Viewport width: " + viewportWidth);
    console.log("Viewport height: " + viewportHeight);
  </script>
</body>
</html>

Event 对象坐标属性

1. 解释不同坐标系统
  • clientXclientY:相对于视口左上角的坐标。
  • screenXscreenY:相对于屏幕左上角的坐标。
  • offsetXoffsetY:相对于事件目标元素的坐标。
  • pageXpageY:相对于整个文档的坐标。
  • xy:相对于视口左上角的坐标(与 clientXclientY 类似)。
<!DOCTYPE html>
<html>
<head>
  <title>Event Coordinates Example</title>
</head>
<body>
  <div id="eventElement" style="width: 200px; height: 200px; background-color: lightblue;">
    Click me!
  </div>
  <script>
    document.getElementById("eventElement").addEventListener("click", function(event) {
      console.log("clientX: " + event.clientX);
      console.log("clientY: " + event.clientY);
      console.log("screenX: " +

 event.screenX);
      console.log("screenY: " + event.screenY);
      console.log("offsetX: " + event.offsetX);
      console.log("offsetY: " + event.offsetY);
      console.log("pageX: " + event.pageX);
      console.log("pageY: " + event.pageY);
      console.log("x: " + event.x);
      console.log("y: " + event.y);
    });
  </script>
</body>
</html>

实际应用案例

1. 展示如何使用这些属性来实现响应式设计

响应式设计需要根据视口尺寸动态调整布局。

<!DOCTYPE html>
<html>
<head>
  <title>Responsive Design Example</title>
  <style>
    body {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    #responsiveBox {
      width: 50%;
      height: 100px;
      background-color: lightcoral;
    }
  </style>
</head>
<body>
  <div id="responsiveBox">Responsive Box</div>
  <script>
    function adjustBoxSize() {
      var viewportWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
      var box = document.getElementById("responsiveBox");
      if (viewportWidth < 600) {
        box.style.width = "80%";
      } else {
        box.style.width = "50%";
      }
    }
    window.addEventListener("resize", adjustBoxSize);
    adjustBoxSize();
  </script>
</body>
</html>
2. 举例说明如何根据屏幕尺寸调整布局
<!DOCTYPE html>
<html>
<head>
  <title>Layout Adjustment Example</title>
  <style>
    .container {
      display: flex;
      flex-wrap: wrap;
    }
    .box {
      flex: 1 1 200px;
      height: 100px;
      margin: 10px;
      background-color: lightseagreen;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
</body>
</html>

浏览器兼容性和注意事项

1. 讨论不同浏览器之间的差异

尽管现代浏览器对上述属性和方法的支持较为一致,但仍需注意一些小差异。比如,IE 浏览器对于某些事件属性的处理不同,需进行兼容性测试。

2. 提供一些最佳实践和技巧
  • 使用 windowdocument 的尺寸属性时,优先考虑兼容性写法。
  • 在进行坐标计算时,充分了解不同坐标系统的差异。
  • 测试代码在多种浏览器中运行情况,确保一致性。
  • 利用 CSS 和 JavaScript 联合实现响应式设计,提升用户体验。

标签:body,console,log,screen,width,window,HTML,document
From: https://blog.csdn.net/imdeity/article/details/139230863

相关文章

  • Windows下安装配置深度学习环境
    Windows下安装配置深度学习环境1.准备工作1.1环境准备操作系统:win1022H2GPU:NvidiaGeForceRTX306012G1.2安装Nvidia驱动、cuda、cuDNN下载驱动需要注册并登录英伟达账号。我这里将下面用到的安装包放到了百度网盘,可以关注微信公众号思......
  • html解决浏览器记住密码输入框的问题
    浏览器通常会记住用户在表单中输入的信息,包括密码字段。这是通过表单的autocomplete属性来控制的。如果你希望浏览器不要记住密码字段的输入,可以设置autocomplete属性为off或new-password。以下是一个HTML表单示例,展示如何禁止浏览器记住密码字段: <!DOCTYPEhtml><htmllang......
  • HTML+CSS实现全景轮播的示例代码
    创建一个全景轮播效果可以通过HTML和CSS来实现,这里提供一个简单的示例代码。这个示例中,我们将使用HTML来构建基本的轮播结构,CSS来添加样式和实现轮播效果。<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-wi......
  • 内网渗透-Windows常用提权方法
    一、前言将介绍常见的提权方法。从为什么该方法能够提权(原理)到使用方法。二、系统内核漏洞提权1.为什么能提权?内核漏洞通常是指内核溢出漏洞,什么溢出呢?缓冲区溢出。那什么是缓冲区溢出呢?当应用程序或者是内核的驱动程序在接受用户输入的数据的时候,它会把这些数据存储在缓冲区......
  • 一个免费、时尚、强大的 Windows GitHub 客户端
    前言今天大姚给大家分享一个.NET开源(MITLicense)、免费、时尚、功能强大的WindowsGitHub客户端:FluentHub。工具功能多任务标签页。上下文菜单扩展。对问题和PR进行评论。用户/组织/代码库页面。代码库页面的列/树布局。编辑用户固定的代码库。编辑用户个人资料信息。......
  • WindowsCA证书服务(二)IIS发放证书
    简介IIS,虽说没怎么用,asp也少了,即使有也DOCKER部署。但是和CA中心配合,最合适的就IIS了吧。安装安装windowsserver2022略加入域略安装IIS添加IIS 角色服务先默认 测试IIShttp 检查根证书运行certmgr,可以看到加入域的计算机是自动获取自己的根证书的。手动申......
  • 创建HTML简单页面
    一.<!--在vscode中用html语言编写一个html页面网站-->   <!--创建的页面主题为:端午那些事-->1.代码如下:2.上面的代码显示出的页面如下:3.因为不能发视频,所以粗糙地截了下图。解释:上面的带下划线的字体是可以跳转页面的,开始的五行是可以在通过锚点来跳转......
  • (读后分享)移动Web前端高效开发实战:HTML 5 + CSS 3 + JavaScript + Webpack + React Nat
    链接:pan.baidu.com/s/1tIHXj9HmIYojAHqje09DTA?pwd=jqso提取码:jqsoHTML5新特性与应用:介绍HTML5的新特性,包括语义化标签、本地存储、设备兼容、连接特性等,并讲解如何在移动Web前端开发中充分利用这些特性提升用户体验。CSS3样式与动画设计:详细讲解CSS3的样式设计和动画效果,包......
  • 【MySQL数据库】认识数据库+环境搭建--------Windows系统
    一、认识数据库数据库(Database)是按照数据结构来组织、存储和管理数据的仓库。二、MySQL数据库MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,目前属于Oracle公司。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大......
  • Windows、Linux下,基于QT的打包方法
    整理这篇文档的意义在于:自己走了很多弯路,淋过雨所以想为别人撑伞,也方便回顾,仅供参考ps:第一次做Windows下打包,用了2小时,第二次20秒第一次做Linux(ubuntu)下打包,用了8小时,第二次1分半一、Windows有许多比较坑的帖子,会带新人走不少弯路,大家注意鉴别(没方法,随缘)1、首先,找到......