Home Web Front-end HTML Tutorial 关于浏览器window、document、html、body高度的探究_html/css_WEB-ITnose

关于浏览器window、document、html、body高度的探究_html/css_WEB-ITnose

Jun 24, 2016 am 11:32 AM

  首先说明本人所理解的这几个元素的计算

  window高度应当是文档所在窗口的可视高度(没有包括浏览器的滚动条),计算方法document.documentElement.clientHeight

  document高度应该为文档内容的高度,计算方法Math.max(document.body[ "scrollHeight" ], document.documentElement[ "scrollHeight"])

  html高度应当为html元素的高度(包括边框滚动条),计算方法htmlElement.offsetHeight

  body高度是body元素的高度(包括边框滚动条),计算方法bodyElements.offsetHeight;

  以上测试方式和jQuery的处理相同。直接使用jQuery来测试。

 

  用例:

  注意:里面的#absolute是脱离文档流的元素。

  测试必要条件:

  1.所有测试都在没有设定margin/padding为整数的情况下测定。

  2.其中细节高度笔者使用QQ截图工具取像素对比

<!DOCTYPE html><html lang="ch-cn">  <head>  <meta charset="utf-8">  <script type="text/javascript" src='jquery-1.9.1.js'></script>    <style type="text/css">    html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{      margin: 0;      padding:0;    }    #absolute{      position: absolute;      margin-left: 300px;      height: 500px;      width: 500px;      background: #ff0;    }    #normal{      height: 300px;      width: 300px;      background: #f00;    }    </style>  </head>  <body>  <div id='absolute' >我是absolute</div>  <div id="normal">我是normal</div>  <script type="text/javascript">    function show(){      console.log("window height " + $(window).height());      console.log("document height " + $(document).height());      console.log("html height " + $("html").height());      console.log("body height " + $("body").height());    }  </script>  </body>  </html>
Copy after login

  显示效果为

  

  第一组测试:IE8

  IE8总结:

  window可视窗口高度,不包括浏览器滚动条(IE后面两个图对比可得,第二个IE下面有个滚动条占6px)。IE有一个特点:window高度与真正的可视窗口相比会少4px(第三个IE图片可知,这个时候document的高度和可视窗口高度相同),说明IE的window四边有2px的间隙,也就是IE自己的offset值。

  document文档高度,完全显示所有文档内容的高度(非iframe内【包括脱离文档流的元素显示】,第一个IE图可得)和IE工作区(看html解释)取最大值(这个高度最终决定窗口滚动条能滚动显示到哪里)

  html高度是窗口的高度,这个窗口是IE自己划定的浏览器工作区域高度,包括可视窗口、2px的offset、浏览器滚动条等等,看下面的效果图

  

  body高度是内容真实高度(脱离文档流定位的不再里面,三个图可以为此作证)

 

  第二组测试:IE9+

  

  

  

 

  IE9+总结:

  window高度是可视窗口高度,不包括浏览器滚动条。

  document文档高度,完全显示所有文档内容的高度(非iframe内【包括脱离文档流的元素显示】,第一个IE图可得)和IE工作区(看html解释)取最大值(这个高度最终决定窗口滚动条能滚动显示到哪里)

  html高度是窗口的高度,这个窗口是IE自己划定的浏览器工作区域高度,包括可视窗口、浏览器滚动条,比IE8要标准一些(没有offset了,滚动条和工作区边也没有了间隙,看下图)

  

  body高度内容真实高度(脱离文档流定位的不再里面)

 

  第二组测试:chrome

  chrome总结:

  window高度是可视窗口高度,不包括浏览器滚动条。

  document高度是完全显示所有文档内容的高度(非iframe内【包括脱离文档流的元素显示】,第一个chrome图可得)和可视窗口高度取最大值(这个高度最终决定窗口滚动条能滚动显示到哪里)

  html高度和body内容高度相同

  body高度内容真实高度(脱离文档流定位的不再里面)

 

  第三组测试:Firefox

  Firefox总结:

  window高度是可视窗口高度,不包括浏览器滚动条。

  document高度是完全显示所有文档内容的高度(非iframe内【包括脱离文档流的元素显示】,第一个Firefox图可得)和可视窗口高度取最大值(这个高度最终决定窗口滚动条能滚动显示到哪里)

  html高度和body内容高度相同

  body高度内容真实高度(脱离文档流定位的不再里面)

 

  好了,总结一下:

  window高度是可视窗口高度,不包括浏览器滚动条高度。比较特别的是IE8的可视窗口边上有2px的offset,所以IE8的可视窗口高度会比我们看到的窗口要小4px。

  document高度是完整文档高度,这个高度计算根据浏览器不同而不同:标准浏览器document高度 = max(文档真实完整高度【包括脱离文档流的元素的显示】,可视窗口高度);IE浏览器document高度 = max(文档真实完整高度【包括脱离文档流的元素的显示】,IE工作区高度)

  html高度根两种:标准浏览器html高度=body内容高度;IE浏览器html高度 = IE工作区高度(具体查看IE的分析);

  body高度是内容真实高度(脱离文档流定位的不再里面)。

  

  对比总结中可以看出,IE和标准浏览器区别最大的是document和html的计算方法。有一点要说明的是,IE的documen高度因为可能是IE工作区高度,那么可能会包括滚动条,如IE9+测试的第二个图可以看出。

 

  这种分析最耗时间,如果大家觉得不错就别光看不顶了。

 

  如果觉得本文不错,请点击右下方【推荐】!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

See all articles