


1. Get the inline style of the element
var obj = document.getElementById("test");
alert(obj.height "n" obj.width);
// 200px 200px typeof=string just displays the value in the style attribute
2. Get the calculated style
var obj = document.getElementById("test");
var style = null;
if (window.getComputedStyle) {
Style = window.getComputedStyle(obj, null); // Non-IE
} else {
Style = obj.currentStyle; // IE
}
alert("width=" style.width "nheight=" style.height);
Note: If the width and height of the element are not set, the default width and height will be returned in non-IE browsers. Return auto string
under IE3. Get the styles written in the and
var obj = document.styleSheets[0]; // [object StyleSheetList] Number of style sheetsvar rule = null;// [object CSSRule]
if (obj.cssRules){
Rule = obj.cssRules[0]; // Non-IE [object CSSRuleList]
} else {
Rule = obj.rules[0]; // IE [object CSSRuleList]
}
alert(rule.style.width);
cssRules (or rules) can only obtain the width and height of inline and link styles, but cannot obtain inline and calculated styles.
Summary: The above three CSS methods of obtaining element size can only obtain the CSS size of the element, but cannot obtain the actual size of the element itself. For example, adding padding, scroll bars, borders, etc.
4. Get the actual size of the element
1. clientWidth and clientHeight
This set of attributes can get the size of the element's visible area, and the space occupied by the element's content and padding. The element size is returned, but there is no unit. The default unit is px. If you forcibly set the unit, such as 100em, it will still return the size of px. (If CSS is obtained, it will be obtained according to the style you set). For the actual size of the element, clientWidth and clientHeight are understood as follows:
a. Add border, no change;
b. Add margin, no change;
c. Increase the scroll bar, and the final value is equal to the original size minus the size of the scroll bar;
d. Increase the padding, the final value is equal to the original size plus the size of the padding;
#test{
background-color: green;
Width: 200px;
height: 200px;
Border: solid 5px red; /* Corresponds to a understanding, result: 200,200 */
Margin: 10px; /* Corresponds to b understanding, result: 200,200*/
Padding: 20px; /* Corresponds to c understanding, result: 240,240*/
Overflow: scroll; /* Corresponding to d understanding, the result is: 223, 223, 223=200 (css size) 40 (padding on both sides) - 17 (scroll bar width) */
}
window.onload = function(){
var obj = document.getElementById("test");
alert(obj.clientWidth "," obj.clientHeight);
};
Note: If no CSS width and height are set, non-IE browsers will include the calculated size of the scroll bar and padding, while IE browsers will return 0 (fixed in IE8).
2. scrollWidth and scrollHeight
This set of properties can get the element size of scrolling content (visible content). Returns the element size, the default unit is px. If you don't set any CSS width and height, it will get the calculated width and height. For the actual size of the element, scrollWidth and scrollHeight are understood as follows:
1. Add borders, different browsers have different interpretations (the following works normally in IE8, but not in IE6):
a) Firefox and Opera browsers will increase the size of the border, 220x220
b) IE, Chrome and Safari browsers will ignore the border size, 200x200
c) IE browser only displays the height of its original content, 200x18 (IE8 has corrected this problem)
2. Add padding, the final value will be equal to the original size plus padding size, 220x220, IE is 220x38
3. Add a scroll bar, the final value will be equal to the original size minus the scroll bar size, 184x184, IE is 184x18
4. Added external border strongholds, no change.
5. Increase content overflow. Firefox, Chrome and IE obtain the actual content height. Opera obtains a smaller height than the first three browsers, and Safari obtains a larger height than the first three browsers.
3. offsetWidth and offsetHeight
This set of properties can return the actual size of the element, including borders, padding and scroll bars. Returns the element size, the default unit is px. If you don't set any CSS width and height, it will get the calculated width and height. For the actual size of the element, offsetWidth and offsetHeight are understood as follows:
1. Add a border, the final value will be equal to the original size plus the border size, which is 220;
2. Increase the padding, the final value will be equal to the original size plus the padding size, which is 220;
3. Added external border strongholds, no change;
4. Add scroll bar, no change, no decrease;
For obtaining the element size, it is generally more convenient to use block-level elements and elements with CSS sizes set. It is particularly troublesome if it is an inline element (inline) or an element with no set size, so it is recommended to pay attention when using it.
#test{
background-color: green;
Width: 200px;
height: 200px;
Border: solid 10px red; /*Result: 220,220*/
Margin: 10px; /*Result: 220,220 (no change)*/
Padding: 10px; /*Result: 240,240*/
Overflow: scroll; /*Result: 240,240 (no change)*/
}
window.onload = function(){
var obj = document.getElementById("test");
alert(obj.offsetWidth "," obj.offsetHeight);
};
5. Get the surrounding size of the element
1. clientLeft and clientTop get the border size
This set of attributes can get the size of the left border and top border set by the element. Currently, only the Left and Top groups are provided, and Right and Bottom are not provided. If the width of the four sides is different, it can be obtained directly through the calculated style, or by subtraction of the above three groups to obtain the element size.
The width of the right border: obj.offsetWidth-obj.clientWidth-obj.clientLeft
Bottom border width: obj.offsetHeight-obj.clientHeight-obj.clientTop
#test{
background-color: green;
Width: 200px;
height: 200px;
Border-top: solid 10px red;s
Border-right: solid 20px #00ff00;
Border-bottom: solid 30px blue;
Border-left: solid 40px #808080;
}
window.onload = function(){
var obj = document.getElementById("test");
alert(obj.clientLeft "," obj.clientTop); // 40,10
};
2. offsetLeft and offsetTop
This set of attributes can obtain the position of the current element relative to the parent element. To get the current position of an element relative to its parent element, it is best to set it to position:absolute; otherwise different browsers will have different interpretations.
a. Set position to absolute, and all browsers will return the same value. Such as:
#test{
background-color: green;
Width: 200px;
height: 200px;
Position: absolute;
Left: 30px;
top: 20px;
}
window.onload = function(){
var obj = document.getElementById("test");
alert(obj.offsetLeft "," obj.offsetTop); // 30, 20
};
b. Adding borders and padding will not affect its position, but adding outer padding will add up.
3. box.offsetParent gets the parent element
In offsetParent, if the parent element is
If, in many layers, the outer layer has been positioned, how do we get the distance between the inner layer elements and the body or html elements? That is, get the position of any element on the page. Then we can write a function to achieve accumulation by continuously backtracking upwards.
box.offsetTop box.offsetParent.offsetTop; // When there are only two layers
function offsetLeft(element){
var left = element.offsetLeft; // Get the first layer distance
var parent = element.offsetParent; // Get the first parent element
While (parent !== null) { // If there is a previous parent element
left = parent.offsetLeft; // Accumulate the distance of this layer
parent = parent.offsetParent; // Get the parent element of this layer
} //Then continue the loop
Return left;
}
4.scrollTop and scrollLeft
This set of attributes can obtain the size of the area where the scroll bar is hidden (the area above the scroll bar), and can also be set to locate this area. If you want the scroll bar to scroll to the initial position, you can write a function:
function scrollStart (element) {
If ( element.scrollTop != 0 ) {
element.scrollTop = 0;
}
}
5. getBoundingClientRect()
This method returns a rectangle object containing four properties: left, top, right and bottom. Represents the distance between each side of the element and the top and left sides of the page respectively.
var box=document.getElementById('box'); // Get element
alert(box.getBoundingClientRect().top); // The distance between the top of the element and the top of the page
alert(box.getBoundingClientRect().right); //The distance between the right side of the element and the left side of the page
alert(box.getBoundingClientRect().bottom); //The distance between the bottom of the element and the top of the page
alert(box.getBoundingClientRect().left); //The distance between the left side of the element and the left side of the page
Note: IE, Firefox3, Opera9.5, Chrome, and Safari are supported. In IE, the default coordinates are calculated from (2,2), resulting in the final distance being two pixels more than other browsers. We need to make a compatible.
document.documentElement.clientTop; //Non-IE is 0, IE is 2
document.documentElement.clientLeft; //Non-IE is 0, IE is 2
functiongGetRect (element) {
var rect = element.getBoundingClientRect();
var top = document.documentElement.clientTop;
var left= document.documentElement.clientLeft;
Return{
TOP: RECT.TOP -TOP,
bottom : rect.bottom - top,
left Right : rect.right - left
}
}
The above is all the content described in this article, I hope you guys will like it.

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
