Home Web Front-end JS Tutorial js implementation code to obtain the size of the browser's visual area_javascript skills

js implementation code to obtain the size of the browser's visual area_javascript skills

May 16, 2016 pm 05:58 PM
size

测试例子:

复制代码 代码如下:

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











结果:

chrome:


FF

OPERA:

SAFARI:

IE9:

IE8

IE7:

IE6

说明:

Chrome/FF/Safari/opera<br>对这些浏览器而言,window有个属性innerWidth/innerHeight包含的是整个文档的可视区域尺寸,注意,这个尺寸是包含滚动条大小的。<br>如果我们不计滚动条的影响,就可以直接使用这两个属性。<br>如果滚动条会影响(比如最大化弹出框),那么应该想另外的办法。<br><br>Document对象是每个DOM树的根,但是它并不代表树中的一个HTML元素,document.documentElement属性引用了作为文档根元素的html标记,document.body属性引用了body标记<br>我们这里获取常见的三个值(scrollWidth、offsetWidth和clientwidth)来比较一下<br><br>document.documentElement.scrollWidth返回整个文档的宽度<br>document.documentElement.offsetWidth返回整个文档的可见宽度<br>document.documentElement.clientwidth返回整个文档的可见宽度(不包含边框),clientwidth = offsetWidth - borderWidth<br>不过一般来说,我们不会给document.documentElement来设置边框,所以这里的clientwidth 与 offsetWidth一致<br><br>document.body.scrollWidth返回body的宽度<br><span style="COLOR: #ff0000">注意,这里的scrollWidth有个不一致的地方,基于webkit的浏览器(chrome和safari)返回的是整个文档的宽度,也就是和document.documentElement.scrollWidth一致,</span><br>opera和FF返回的就是标准的body 的scrollWidth,个人觉得opera和FF算是比较合理的。<br>document.body.offsetWidth返回body的offsetWidth<br>document.body.clientwidth返回body的clientwidth(不包含边框),clientwidth = offsetWidth - borderWidth<br><br>我们看上面的例子,会发现body和documentElement的有些值是相等的,这并不是表示他们是等同的。而是因为当我们没有给body设置宽度的时候,document.body默认占满整个窗口宽度,于是就有:<br>document.body.scrollWidth = document.documentElement.scrollWidth<br>document.body.offsetWidth = document.documentElement.offsetWidth<br>document.body.clientwidth = document.documentElement.clientwidth - document.body.borderWidth(body的边框宽度)<br>当我们给body设置了一个宽度的时候,区别就出来了。<br><br>IE9/IE8<br>这两个差不多,唯一的区别是IE9包含window.innerWidth属性,而IE8不包含window.innerWidth属性。<br>document.documentElement.scrollWidth返回整个文档的宽度,和FF等浏览器一致<br>document.documentElement.offsetWidth返回整个文档的可见宽度(包含滚动条,值和innerWidth一致),注意,这里和FF等浏览器又有点区别。<br>document.documentElement.clientwidth返回整个文档的可见宽度(不包含边框),和FF等浏览器一致。clientwidth = offsetWidth - 滚动条宽度<br><br>document.body.scrollWidth返回body的宽度,<span style="COLOR: #ff0000">注意,这里的scrollWidth和FF等浏览器有点区别,这里并不包括body本身的border宽度。</span><br><span style="COLOR: #ff0000">因此例子中,相比FF少了10px。</span><br>document.body.offsetWidth返回body的offsetWidth,和FF等浏览器一致<br>document.body.clientwidth返回body的clientwidth(不包含边框),和FF等浏览器一致,clientwidth = offsetWidth - borderWidth<br><br>IE7<br>IE7与IE9/IE8的主要区别是<br>第一、document.documentElement.offsetWidth的返回值不一样,<br>参见上面说的,IE9/IE8的document.documentElement.offsetWidth包含滚动条,<span style="COLOR: #ff0000">但是,IE7的document.documentElement.offsetWidth不包含滚动条。</span><br>第二、document.documentElement.scrollWidth返回整个文档的宽度,<span style="COLOR: #ff0000">注意,这里和IE9/IE8、FF等浏览器又有不一致,对于IE9/IE8、FF等浏览器,scrollWidth最小不会小于窗口的宽度,但是在IE下没有这个限制,文档有多小,这个就有多小</span><br>其他倒是挺一致的。<br><br>最后是IE6了<br>IE6的document.documentElement返回值与IE9/IE8没有区别(由此可见,对于document.documentElement,IE7就是个奇葩)。<br><span style="COLOR: #ff0000">话说回来,IE的document.body就是个奇葩,当没有给body设置宽度的时候,body是默认占满整个文档的(注意,其他的浏览器都是占满整个窗口),当然,最小值是整个窗口的大小,就是说body指向了根元素。</span><br>因此,在算上IE6在解析width方面的bug,和其他的浏览器的区别就淋漓尽致了。<br>document.body.scrollWidth返回body的宽度,和IE9/IE8/IE7一致<br>document.body.offsetWidth返回body的offsetWidth,注意,由于body的不同,这里的offsetWidth = scrollWidth + borderWidth<br>document.body.clientwidth返回body的clientwidth(不包含边框)clientwidth = offsetWidth - borderWidth<br><span style="COLOR: #ff0000">另外,有一点和IE7同样,就是document.documentElement.scrollWidth没有最小宽度限制。</span>
Copy after login

总结一下,在标准模式下,我们获取文档可见区域的方法如下:

复制代码 代码如下:

function getViewSizeWithoutScrollbar(){//不包含滚动条
return {
width : document.documentElement.clientWidth,
height: document.documentElement.clientHeight
}
}
function getViewSizeWithScrollbar(){//包含滚动条
if(window.innerWidth){
return {
width : window.innerWidth,
height: window.innerHeight
}
}else if(document.documentElement.offsetWidth == document.documentElement.clientWidth){
return {
width : document.documentElement.offsetWidth,
height: document.documentElement.offsetHeight
}
}else{
return {
width : document.documentElement.clientWidth getScrollWith(),
height: document.documentElement.clientHeight getScrollWith()
}
}
}

getScrollWith()是获取滚动条尺寸,参见
http://www.jb51.net/article/29036.htm
有什么错误欢迎指出
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)

What is the size of a4 paper in centimeters? What is the size of a4 paper in centimeters? Feb 11, 2024 pm 11:51 PM

A4 paper is now used in many places, such as office, study, etc. The paper used for copying is basically A4 paper. Many friends don’t know how many centimeters the size of A4 paper is. Let’s take a look. Bar. 1. Size A4 paper is 21cm × 29.7cm (210mm × 297mm). The paper size used in most countries in the world adopts this international standard. 2. Origin A4 paper is 210mm×297mm. Reason 1: A rectangle with a length of 2 and a width of The resulting ratio is . However, 297 divided by 210, the result is 1.414, which is close to, and this ratio is exactly a piece of paper

How to calculate computer size How to calculate computer size Mar 20, 2023 pm 01:43 PM

How to calculate computer size: 1. Find a flat and stable table, place the monitor on the table, and keep the monitor stable; 2. Use a ruler to measure the diagonal length; 3. Convert the units between centimeters and feet. , just calculate the monitor size.

What is the size of a4 paper? What is the size of a4 paper? Apr 25, 2024 am 11:55 AM

1. The aspect ratio of A4 paper is different from the common 2:3 or 3:4. Its unique ratio of √2:1 makes it stand out among many paper sizes. 2. This special ratio provides ideal visual effects and practicality for A4 paper.

How to reduce the size of photos 'A must-see for beginners: Detailed steps to compress image size' How to reduce the size of photos 'A must-see for beginners: Detailed steps to compress image size' Feb 06, 2024 pm 06:55 PM

Many friends use cameras to record their living habits. Some professional photography friends may also use SLR cameras to take clearer photos. However, although the photos taken by SLR cameras are clear, for a photo of tens of megabytes, it will undoubtedly increase the pressure on the storage device. So, is there any way to reduce the size of the photo while retaining the details of the picture as much as possible? In fact, just compress the image. So how to compress the image size? Here are two good methods to share. Method 1: We can import the image in the beauty tool without any other processing and editing. When saving a picture, the system will provide image quality adjustment options, and we can drag the slider to adjust the retained sharpness. When the estimated size meets your needs, click Save. Method 2: ①. First open this "

What is the size of a5 What is the size of a5 Apr 24, 2024 pm 05:53 PM

1. A5 paper is an international standard paper size, its size is 148 mm x 210 mm (5.83 inches x 8.27 inches). 2. It is often used in various applications such as letterheads, notebooks, leaflets, and books. 3. The size of A5 paper is slightly larger than half of A4 paper, and it is the fifth size in the A series of paper sizes. 4. This series of sizes was developed by German engineer Walter Portmann in the early 20th century to create a series of standard paper sizes based on the golden ratio.

What are the dimensions of Huawei P40: length, width and height? What are the dimensions of Huawei P40: length, width and height? Jun 26, 2023 am 11:31 AM

The size of Huawei p40 is 148.9 mm long, 71.06 mm wide, 8.50 mm thick, and weighs about 175 grams including the battery. It is a smartphone developed by Huawei and was released online globally on March 26, 2020. It is available in Frost Silver, Dawn Gold, Available in 5 colors including zero white, bright black, and deep sea blue, it is equipped with Kirin 9905G SoC chip, which combines the processor and baseband into one, innovatively designed CPU three-level energy efficiency architecture, 16-core GPU super cluster, energy efficiency and transistor density, supporting more All 5G bands.

How to change image dimensions and size using Python How to change image dimensions and size using Python Aug 17, 2023 pm 11:21 PM

Introduction to how to use Python to change the size and size of images: In many application scenarios, we may need to change the size and size of images to adapt to different needs. Python provides a wealth of libraries to process and manipulate images, allowing us to easily adjust the size and size of images. This article will introduce how to use Python to change the size and shape of images, and provide code examples for readers' reference. Using the PIL library PIL (PythonImagingLibrary) is Python

Implementation method and detailed explanation of product multi-specification SKU in PHP Implementation method and detailed explanation of product multi-specification SKU in PHP Sep 05, 2023 pm 12:21 PM

Implementation method and detailed explanation of multi-specification SKU of goods in PHP In e-commerce platforms, multi-specification SKU (stock keeping unit) of goods is very common. SKU can be understood as a combination of different attributes of a product, such as size, color, style, etc. Different SKUs correspond to different inventory amounts, and customers need to select a specific SKU when purchasing goods. In PHP, we can use arrays to manage and display multi-specification SKUs of products. This article will introduce the implementation of SKU in detail and provide corresponding code examples. 1. SK

See all articles