Home Web Front-end JS Tutorial js getBoundingClientRect() to get the position of page elements_javascript skills

js getBoundingClientRect() to get the position of page elements_javascript skills

May 16, 2016 pm 06:15 PM
page elements

document.documentElement.getBoundingClientRect

Here is the explanation from MSDN:

Syntax

<EM>oRect</EM> = <EM><SPAN class=moreinfo title="an element from the Applies To list below">object</SPAN></EM><STRONG>.getBoundingClientRect(</STRONG><STRONG>)</STRONG>
Copy after login

Return Value

Returns a TextRectangle object. Each rectangle has four integer properties (top, left, right, and bottom) that represent a coordinate of the rectangle, in pixels.

Remarks

This method retrieves an object that exposes the left, top, right, and bottom coordinates of the union of rectangles relative to the client's upper-left corner. In Microsoft Internet Explorer 5, the window's upper-left is at 2,2 (pixels) with respect to the true client.


To explain it in practice, this method obtains the left, top, right and bottom positions of an element on the page relative to the browser window. It’s not easy to understand either, so I’ll explain it with a picture below.

This method is no longer IE Only. FF3.0 and Opera9.5 already support this method. It can be said that the efficiency of obtaining the position of page elements has been greatly improved. In previous versions of Opera and Firefox You must loop through to get the absolute position of the element on the page.

The following code gives a simple example. You can scroll the scroll bar and click on the red area to see the changes in each value.

Copy code The code is as follows:

.org/1999/xhtml">


Demo



Demo directly uses absolutely positioned elements for convenience

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)

How does JavaScript implement the drag-and-sort function of page elements? How does JavaScript implement the drag-and-sort function of page elements? Oct 20, 2023 pm 12:28 PM

How does JavaScript implement the drag-and-sort function of page elements? Drag sorting is a very common feature in modern web development, which allows users to change the position of elements on the page by dragging them. This article will introduce how to use JavaScript to implement the drag-and-sort function of page elements and provide specific code examples. The basic idea of ​​​​implementing the drag sorting function is as follows: First, create elements that require drag sorting in HTML, such as a set of divs. &lt;

Learn to fix positioning: let page elements move with scrolling and get started quickly Learn to fix positioning: let page elements move with scrolling and get started quickly Jan 20, 2024 am 10:29 AM

Quickly understand the fixed positioning method: making your page elements move with scrolling requires specific code examples. In web design, sometimes we want certain page elements to maintain a fixed position when scrolling and not move with scrolling. This effect can be achieved through CSS fixed positioning (position:fixed). This article will introduce the basic principles of fixed positioning and specific code examples. The principle of fixed positioning is very simple. By setting the positioning attribute of the element to fixed, the element can be fixed at a certain position relative to the viewport.

Use fit-content to achieve horizontally centered layout of page elements Use fit-content to achieve horizontally centered layout of page elements Sep 09, 2023 pm 02:07 PM

Use fit-content to achieve a horizontally centered layout of page elements. In web design, the layout of page elements is a very important part. Implementing a horizontally centered layout of page elements is a common requirement. To solve this problem, we can use the CSS fit-content property. fit-content is a property in CSS that dynamically calculates the width or height of an element based on its content. By setting the element's width or height to fit-content,

Master the common attribute values ​​​​of absolute positioning and let your page elements be placed as you like Master the common attribute values ​​​​of absolute positioning and let your page elements be placed as you like Jan 18, 2024 am 10:01 AM

Master the common attribute values ​​​​of absolute positioning, so that your page elements can be placed as you like. Specific code examples are required. Absolute positioning (absolutepositioning) is one of the commonly used positioning methods in CSS. It allows us to position elements relative to their nearest positioning attributes. The parent element is positioned. By mastering the common attribute values ​​of absolute positioning, we can easily control the position and layout of page elements. 1. Basic concepts of positioning elements Before using absolute positioning, we need to understand some basic concepts. The parent element refers to the

PHP and WebDriver Extensions: How to implement detection and assertion of page elements PHP and WebDriver Extensions: How to implement detection and assertion of page elements Jul 07, 2023 am 09:05 AM

PHP and WebDriver extension: How to implement detection and assertion of page elements Introduction: With the increase of web applications, automated testing has become more and more important. During testing, we need to verify that elements on the page exist and that their state is as expected. This article will introduce how to use PHP and WebDriver extensions to implement detection and assertion of page elements. 1. Preparation Before starting, we need to ensure that the following environment has been configured: Install PHP and Composer Install W

What are the page elements in PHP? What are the page elements in PHP? Aug 23, 2023 pm 01:07 PM

Page elements in PHP include titles, paragraphs, hyperlinks, images, tables, forms, buttons, lists, text boxes, drop-down menus, check boxes, radio buttons, text areas, and paging. Detailed introduction: 1. The title is used to define the main content of the web page. You can use HTML's <h1> to <h6> tags to create different levels of titles; 2. Paragraphs are used to organize and display text content. You can use HTML's <p > tag to create paragraphs; 3. Hyperlinks are used to navigate between different pages. You can use HTML's <a> tag to create hyperlinks, etc.

How to use fit-content technology to center page elements horizontally How to use fit-content technology to center page elements horizontally Sep 08, 2023 pm 12:55 PM

How to use fit-content technology to center page elements horizontally In web design and development, horizontal centering is a common and important layout technology. In the past, we often used margin:0auto to achieve horizontal centering. However, as new layout properties in CSS continue to appear, we now have more choices. One of the powerful techniques is to use the fit-content attribute. It can automatically calculate a suitable width based on the actual content size of the element to achieve horizontal centering.

A deep dive into the criteria for sticky positioning: How to achieve fixed positioning of page elements? A deep dive into the criteria for sticky positioning: How to achieve fixed positioning of page elements? Feb 02, 2024 pm 02:03 PM

A deep dive into the criteria for sticky positioning: How to achieve fixed positioning of page elements? Introduction: In web design, sticky positioning is a very practical technology that can keep page elements in a fixed position when scrolling. It improves user experience and makes pages more dynamic and easier to use. This article will delve into the standards and implementation methods of sticky positioning, and provide specific code examples. 1. The concept of sticky positioning: Sticky positioning is a positioning method in CSS that allows page elements to be aligned when scrolling.

See all articles