JavaScript DOM element size and position_Basic knowledge
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;

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











The environment variable function is an essential tool for running the configuration program in the system. However, in the latest win11 system, there are still many users who do not know how to set it up. Here is a detailed introduction to the location of the win11 environment variable opening. Come and join us. Learn to operate it. Where are the win11 environment variables: 1. First enter "win+R" to open the run box. 2. Then enter the command: controlsystem. 3. In the system information interface that opens, select "Advanced System Settings" from the left menu. 4. Then select the "Environment Variables" option at the bottom of the "System Properties" window that opens. 5. Finally, in the opened environment variables, you can make relevant settings according to your needs.

Every Windows system has a startup path. If you add files or software to it, it will be opened at boot time. However, many friends don’t know where the win11 startup path is. In fact, we only need to enter the corresponding folder on the C drive. Win11 startup path: 1. Double-click to open "This PC" 2. Directly paste the path "C:\ProgramData\Microsoft\Windows\StartMenu\Programs\Startup" into the path box. 3. Here is the win11 startup path. If we want to open the file after booting, we can put the file in. 4. If you cannot enter according to this path, it may be hidden.

The Credential Manager is a function used by users to manage web credentials and Windows credentials, but many users still don’t know where the Windows 10 Credential Manager is. In fact, the credential manager is on the operation panel. After opening the control panel, remember to change the viewing method to a small icon, so that you can see the credential manager. Click View to view all kinds of information. If you want to view a large amount of , you need to enter the account password. Where is the Windows 10 Credential Manager: 1. Open the Control Panel in the system, click the View method in the upper right corner, and convert the type to a small icon. 2. After viewing it as a small icon, click "Certificate Manager". 3. After entering the credential manager, you can see an introduction to the relevant functions, which are mainly used for

There are a total of 20 origami birds in Croaker Film and Television Park on Star Dome Railway. Many players don’t know where the origami birds are in Crocker Film and Television Park. The editor has summarized the locations of each origami bird to help everyone. Search for it, and take a look at this latest summary of the locations of the origami birds in Croaker Film and Television Park for specific content. Guide to the Honkai Star Dome Railway: Origami Bird in Crook Movie Park Location 1, Crook Movie Park 1st Floor 2, and Crook Movie Park 2nd Floor Star Dome Railway

To learn more about the storage location of packages installed by pip, you need specific code examples. Pip is a commonly used package management tool in the Python language. It is used to easily install, upgrade and manage Python packages. When using pip to install a package, it will automatically download the corresponding package file from PyPI (Python Package Index) and install it to the specified location. So, where are the packages installed by pip stored? This is a problem that many Python developers will encounter. This article will delve into the location of the packages installed by pip and provide

Apple in iOS 17 has added a new feature in Messages to let loved ones know when you're home safely. It's called check-in, and here's how you use it. Whether you're walking home after dark or going for an early morning run, you can start checking in with family or friends in Apple's Messages app to let them know when you're home safely. Upon your arrival, CheckIn automatically detects when you are home and notifies your friends. When they are alerted and the check-in has ended, you will also be notified. If something unexpected happens and you're delayed en route, CheckTab will even recognize that you're not making progress and check in with you, asking if you want to increase your ETA. if you don't have

If we are going to be away from the computer for a long time, it is best to shut down the computer to protect it. So where is the shutdown in win11? In fact, generally speaking, just open the start menu and you can find the shutdown button in it. Where to shut down Windows 11: Answer: In the power button of the start menu. 1. First, we click the "Windows Logo" on the bottom taskbar to open the "Start Menu" 2. After opening, you can find the "Power" button in the lower right corner, as shown in the figure. 3. After clicking the power button, you will see "Shutdown", click it to shut down. 4. If the computer cannot be shut down due to special circumstances such as a crash, you can directly press and hold the "power button" on the computer to force a shutdown.

In "Last Age", players can play in various modes such as game mode, challenge mode, and arena, etc. Arena is the ultimate way to play the game, providing two modes for players to choose from. Where is the Arena in the Last Era? Answer: The Arena is an endgame game, and its specific location is at the Champion's Gate. You need to obtain the Arena Key or Memory Arena Key. After right-clicking, you can see the world map and find the specific location of the Champion's Gate. The arena is divided into two major modes: Arena Championship Mode and Endless Arena Mode. The former includes 40 waves of enemies and selected rewards, always culminating in a battle with the Arena Champion. There are 4 stages in Arena Championship Mode. The higher the difficulty, the better the rewards. Endless Arena is a mode with infinite waves. The difficulty gradually increases. The challenger with the best score will
