


Summary of getting height and width functions in JavaScript_javascript tips
html code:
& Lt; h3 & gt; This is the parent element, the screen resolution is 1366*768 & lt;/h3 & gt;
259280570
Welcome to join
Happy National Day
css:
Copy code
The code is as follows:border: 5px solid red;
float: left;
}
.son
{
width: 400px;
height: 300px;
border: 5px solid black;
Margin: 20px;
}
.grandson
{
width: 150px;
height: 100px;
border: 5px solid blue;
Margin: 20px;
overflow: auto;
}
.data
{
width: 600px;
height: 750px;
border: 5px solid red;
float: left;
margin-left: 15px;
}
js:
Copy code
The code is as follows:window.onload = function()
{
/*Get element object*/
var father = document.getElementById('father');
var son = document.getElementById('son');
var grandson = document.getElementById('grandson');
var data = document.getElementById('data');
data.innerHTML = "
Get the window size (related to the window size)
";data.innerHTML = "Document.body visible area width: " document.body.clientWidth "
";
data.innerHTML = "Document.body visible area height: " document.body.clientHeight "
";
data.innerHTML = "window.innerWidth visible area width: " window.innerWidth "
";
data.innerHTML = "window.innerHeight visible area height: " window.innerHeight "
";
data.innerHTML = "document.documentElement visible area width: " document.documentElement.clientWidth "
";
data.innerHTML = "document.documentElement visible area height: " document.documentElement.clientHeight "
";
data.innerHTML = "
Get the size of the element itself (regardless of whether there is a scroll bar)
";data.innerHTML = ".son's own width (offsetWidth attribute, including left and right borders):" son.offsetWidth "
";
data.innerHTML = ".son's own height (offsetHeight attribute, including upper and lower borders):" son.offsetHeight "
";
data.innerHTML = ".son visual width (clientWidth attribute, excluding left and right borders):" son.clientWidth "
";
data.innerHTML = ".son visual height (clientHeight attribute, excluding upper and lower borders):" son.clientHeight "
";
data.innerHTML = "
Get.grandson scrolling size and visual size
"; data.innerHTML = ".grandson scroll width (scrollWidth attribute):" grandson.scrollWidth "
";
data.innerHTML = ".grandson scroll height (scrollHeight attribute):" grandson.scrollHeight "
";
data.innerHTML = ".grandson visual width (clientWidth attribute, excluding vertical scroll bar and border):" grandson.clientWidth "
";
data.innerHTML = ".grandson visual height (clientHeight attribute, excluding horizontal scroll bar and border):" grandson.clientHeight "
";
data.innerHTML = "
Get the size of .grandson being rolled up (related to the position of the scroll bar)
";data.innerHTML = ".The height to which grandson is scrolled (scrollTop attribute, the vertical scroll bar slides to the bottom):" grandson.scrollTop "
";
data.innerHTML = ".grandson is scrolled to the left (scrollLeft attribute, the horizontal scroll bar slides to the far right):" grandson.scrollLeft "
";
data.innerHTML = "
Get the browser window position (related to the window size)
";/*
*IE, Chrome, Safari, and Opera all provide support for window.screenLeft and *window.screenTop, but Firxfox does not support these two attributes;
*Firxfox, Chrome, Safari, and Opera all provide support for window.screenX *and window.screenY, but IE does not support these two attributes;
*/
var leftPos = (typeof window.screenLeft == 'number')?window.screenLeft:window.screenX;
var topPos = (typeof window.screenTop == 'number')?window.screenTop:window.screenY;
data.innerHTML = "On the body page text part (window.screenTop(Y)):" topPos "
";
data.innerHTML = "The left side of the body text part (window.screenLeft(X)):" leftPos "
";
data.innerHTML = "
Get screen resolution
"; data.innerHTML = "Screen resolution height (window.screen.height):" window.screen.height "
";
data.innerHTML = "Screen resolution width (window.screen.width):" window.screen.width "
";
data.innerHTML = "
Get the available height and width of the screen
";data.innerHTML = "Screen resolution height (window.screen.availHeight):" window.screen.availHeight "
";
data.innerHTML = "Screen resolution width (window.screen.availWidth):" window.screen.availWidth "
";
data.innerHTML = "
Get the border size of .father
";data.innerHTML = ".father upper border (clientTop):" father.clientTop "
";
data.innerHTML = ".father left border (clientLeft):" father.clientLeft "
";
data.innerHTML = "
Get the distance from .son to the border of the parent element (that is, the border corresponding to the margin parent element)
";data.innerHTML = ".son to the upper boundary of the parent element (offsetTop):" son.offsetTop "
";
data.innerHTML = ".son to the left edge of the parent element (offsetLeft):" son.offsetLeft "
";
}
ps: There are differences in how browsers parse boxes, so there will be small differences in the data obtained above. Attached is a picture.
Full code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8" /> <title>test</title> <style type="text/css"> * { margin: 0 auto; } .father { width: 500px; height: 750px; border: 5px solid red; float: left; } .son { width: 400px; height: 300px; border: 5px solid black; margin: 20px; } .grandson { width: 150px; height: 100px; border: 5px solid blue; margin: 20px; overflow: auto; } .data { width: 600px; height: 750px; border: 5px solid red; float: left; margin-left: 15px; } </style> <script type="text/javascript"> window.onload = function() { /*获取元素对象*/ var father = document.getElementById('father'); var son = document.getElementById('son'); var grandson = document.getElementById('grandson'); var data = document.getElementById('data'); data.innerHTML = "<h3>获取视窗大小(跟窗口大小有关)</h3>"; data.innerHTML += "document.body可见区域宽: "+document.body.clientWidth+"<br/>"; data.innerHTML += "document.body可见区域高: "+document.body.clientHeight+"<br/>"; data.innerHTML += "window.innerWidth可见区域宽: "+window.innerWidth+"<br/>"; data.innerHTML += "window.innerHeight可见区域高: "+window.innerHeight+"<br/>"; data.innerHTML += "document.documentElement可见区域宽: "+document.documentElement.clientWidth+"<br/>"; data.innerHTML += "document.documentElement可见区域高: "+document.documentElement.clientHeight+"<br/>"; data.innerHTML += "<h3>获取元素自身大小(跟是否有滚动条无关)</h3>"; data.innerHTML += ".son自身宽度(offsetWidth属性,包括左右边框):"+son.offsetWidth+"<br/>"; data.innerHTML += ".son自身高度(offsetHeight属性,包括上下边框):"+son.offsetHeight+"<br/>"; data.innerHTML += ".son可视宽度(clientWidth属性,不包括左右边框):"+son.clientWidth+"<br/>"; data.innerHTML += ".son可视高度(clientHeight属性,不包括上下边框):"+son.clientHeight+"<br/>"; data.innerHTML += "<h3>获取.grandson滚动大小和可视大小</h3>"; data.innerHTML += ".grandson滚动宽度(scrollWidth属性):"+grandson.scrollWidth+"<br/>"; data.innerHTML += ".grandson滚动高度(scrollHeight属性):"+grandson.scrollHeight+"<br/>"; data.innerHTML += ".grandson可视宽度(clientWidth属性,不包括竖直滚动条和border):"+grandson.clientWidth+"<br/>"; data.innerHTML += ".grandson可视高度(clientHeight属性,不包括水平滚动条和border):"+grandson.clientHeight+"<br/>"; data.innerHTML += "<h3>获取.grandson被卷去的大小(跟滚动条的位置有关)</h3>"; data.innerHTML += ".grandson被卷去的高(scrollTop属性,竖直滚动条滑到最底端):"+grandson.scrollTop+"<br/>"; data.innerHTML += ".grandson被卷去的左(scrollLeft属性,水平滚动条滑到最右端):"+grandson.scrollLeft+"<br/>"; data.innerHTML += "<h3>获取浏览器窗口位置(跟窗口大小有关)</h3>"; /* *IE、Chrome、Safari、Opera 都提供了支持 window.screenLeft 和 *window.screenTop,但是Firxfox不支持这个两个属性; *Firxfox、Chrome、Safari、Opera 都提供了支持 window.screenX *和 window.screenY,但是 IE 不支持这个两个属性; */ var leftPos = (typeof window.screenLeft == 'number')?window.screenLeft:window.screenX; var topPos = (typeof window.screenTop == 'number')?window.screenTop:window.screenY; data.innerHTML += "body网页正文部分上(window.screenTop(Y)):"+ topPos+"<br/>"; data.innerHTML += "body网页正文部分左(window.screenLeft(X)):"+ leftPos+"<br/>"; data.innerHTML += "<h3>获取屏幕分辨率</h3>"; data.innerHTML += "屏幕分辨率的高(window.screen.height):"+ window.screen.height+"<br/>"; data.innerHTML += "屏幕分辨率的宽(window.screen.width):"+ window.screen.width+"<br/>"; data.innerHTML += "<h3>获取屏幕可用的高宽</h3>"; data.innerHTML += "屏幕分辨率的高(window.screen.availHeight):"+ window.screen.availHeight+"<br/>"; data.innerHTML += "屏幕分辨率的宽(window.screen.availWidth):"+ window.screen.availWidth+"<br/>"; data.innerHTML += "<h3>获取.father的边框大小</h3>"; data.innerHTML += ".father上边框(clientTop):"+ father.clientTop+"<br/>"; data.innerHTML += ".father左边框(clientLeft):"+ father.clientLeft+"<br/>"; data.innerHTML += "<h3>获取.son到父元素边界的距离(即对应margin+父元素对应的border)</h3>"; data.innerHTML += ".son到父元素上边界(offsetTop):"+ son.offsetTop+"<br/>"; data.innerHTML += ".son到父元素左边界(offsetLeft):"+ son.offsetLeft+"<br/>"; } </script> </head> <body> <div class="father" id="father"> <h3>这是父元素,屏幕分辨率是1366*768</h3> <div class="son" id="son"> <h3>这是子元素,祝大家国庆快乐 </h3> <div class="grandson" id="grandson"> <h3>这是孙子元素,祝大家国庆快乐 </h3> <h3>我的博客:www.jb51.net</h3> <h3>程序爱好者QQ群:</h3> <h3>259280570 </h3> <h3>欢迎你加入 </h3> <h3>国庆快乐 </h3> </div> </div> </div> <div class="data"> <h3>数据输出</h3> <div id="data"></div> </div> </body> </html>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
