1 Get the CSS size of the element
1. Get the size of the element through style inline
var box = document.getElementById('box'); // Get element;
box.style.width; box.style.height;
// PS: Obtain the size of the element through calculation, regardless of whether it is inline/inline or link, it returns the calculated result;
// If the size is set by itself, it will return the size of the element; if it is not set by itself, non-IE will return the default size, and IE will return auto;
3. Get the size of the element through the cssRules (or rules) attribute in the CSSStyleSheet object;
PS: cssRules 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; such as adding padding/scroll bar/border;
2 Get the actual size of the element
1.clientWidth and clientHeight
This set of attributes can obtain the size of the element's visual area, including the space occupied by the element's content and padding;
box.clientWidth; // 200;
PS: The element size is returned, but there is no unit. The default unit is px;
PS: Regarding the actual size of the element, clientWidth and clientHeight are understood as follows:
1. Add a border to the element, no change, 200;
2. Add outer border to element, no change, 200;
3. Add scroll bar, final value = original size - scroll bar size; 184;
4. Increase padding, final value = original size padding size; 220;
PS: If no CSS width and height are set, then non-IE will include the calculated size of the scroll bar and padding; while IE will return 0;
2.scrollWidth and scrollHeight
This set of attributes can obtain the total height of the element content without scroll bars;
box.scrollWidth;
// PS: Returns the element size, the default unit is px; if no CSS width and height are set, it will get the calculated width and height;
3.offsetWidth and offsetHeight
This set of attributes can return the actual size of the element, including borders/padding and scroll bars;
box.offsetWidth; 200
PS: The element size is returned, and the default unit is px; if no CSS width and height are set, it will get the calculated width and height;
PS: Regarding the actual size of the element, the understanding is as follows:
1. Add border, final value = original size border size; 220;
2. Increase padding, final value = original size padding size; 220;
3. Added external border strongholds, no change;
4. Increase the scroll bar, no change, no decrease;
PS: For obtaining the element size, it is generally more convenient to use block-level elements with CSS sizes set;
3 Get the surrounding size of the element
1.clientLeft and clientTop
// This set of attributes can get the size of the left border and top border set by the element;
box.clientLeft;
2.offsetLeft and offsetTop (offset)
// 这组属性可以获取当前元素边框相对于父元素边框的位置; box.offsetLeft; // 50; // PS:获取元素当前相对于父元素的位置,最好将它设置为定位position:absolute; // PS:加上边框和内边距不会影响它的位置,但加上外边据会累加; box.offsetParent; // 得到父元素; // PS:offsetParent中,如果本身父元素是<body>,非IE返回body对象,IE返回html对象; // 如果两个元素嵌套,如果上级父元素没有使用定位position:absolute,那么offsetParent将返回body或html对象; // 如果说在很多层次里,外层已经定位,获取任意一个元素距离页面上的位置,可以不停的向上回溯获取累加来实现; box.offsetTop+box.offsetParent.offsetTop; // 只有两层的情况下; // 如果多层的话,就必须使用循环或递归; function offsetLeft(element){ var left = element.offsetLeft; // 得到第一层距离; var parent = element.offsetParent; // 得到第一个父元素; while(parent !== null){ // 判断如果还有上一层父元素; left += parent.offsetLeft; // 将得到的距离累加; parent = parent.offsetParent; // 将父元素也回溯; } // 然后循环; return left; // 得到最终距离; }
3.scrollTop and scrollLeft
// 这组属性可以获取被滚动条隐藏的区域大小,也可设置定位到该区域; box.scrollTop; // 获取滚动内容上方的位置; // 设置滚动条滚动到最初始的位置; function scrollStart(element){ if(element.scrollTop != 0){ element.scrollTop = 0; } }
Four getBoundingClientRect() method
// 这个方法返回一个矩形对象,包含四个属性:left/top/right和bottom; // 分别表示元素各边与页面上边和左边的距离; var box = document.getElementById('box'); alert(box.getBoundingClientRect().top); // 元素上边距离页面上边的距离; alert(box.getBoundingClientRect().right); // 元素右边距离页面左边的距离; alert(box.getBoundingClientRect().bottom); // 元素下边距离页面上边的距离; alert(box.getBoundingClientRect().left); // 元素左边距离页面左边的距离; // PS:IE/Firefox/Opera/Chrome/Safari都支持; // 但在IE中,默认坐标从(2,2)开始计算,导致最终距离比其他浏览器多出两个像素; document.documentElement.clientTop; // 非IE为0,IE为2; document.documentElement.clientLeft; // 非IE为0,IE为2; // 兼容getBoundingClientRect() function getRect(element){ var rect = element.getBoundingClientRect(); var top = document.documentElement.clientTop; var left = document.documentElement.clientLeft; return { top:rect.top-top, // 元素上边距-页面的上边距(0-0或2-2); bottom:rect.bottom-top, left:rect.left-left, // 元素左边距-页面的左边距(0-0或2-2); right:rect.right-left } };
Five Summary
1. Offset dimension: includes all visible space occupied by the element on the screen;
The visible size of an element is determined by its height and width, including padding/scrollbars and borders;
2. Client dimension: refers to the space occupied by the element content and its padding;
3.Scroll size (scroll dimension): The size of the element containing the scrolling content;

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


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

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Dreamweaver CS6
Visual web development tools