Detailed explanation of the getBoundingClientRect() method in js

Detailed explanation of the getBoundingClientRect() method in js

1. getBoundingClientRect() Analysis

The getBoundingClientRect() method returns the size of an element and its position relative to the viewport.

grammar

rectObject = object.getBoundingClientRect();

insert image description here

value

rectObject.top: the distance from the top of the element to the top of the window;
rectObject.right: the distance from the right side of the element to the left side of the window;
rectObject.bottom: the distance from the bottom of the element to the top of the window;
rectObject.left: the distance from the left side of the element to the left side of the window;
rectObject.width: is the width of the element itself rectObject.height is the height of the element itself

Cross-browser compatibility

If you need better cross-browser compatibility, use window.pageXOffset and window.pageYOffset instead of window.scrollX and window.scrollY. Scripts that do not have access to these properties can use the following code:

// For scrollX
(((t = document.documentElement) || (t = document.body.parentNode))
  && typeof t.scrollLeft == 'number' ? t : document.body).scrollLeft
// For scrollY
(((t = document.documentElement) || (t = document.body.parentNode))
  && typeof t.scrollTop == 'number' ? t : document.body).scrollTop

Example

// rect is a DOMRect object with four properties: left, top, right, bottom Note: DOMRect is the standard name for TextRectangle or ClientRect, they are the same.
var rect = obj.getBoundingClientRect();

This is the end of this article about the detailed case of the getBoundingClientRect() method in js. For more relevant content about getBoundingClientRect() in js, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • js getBoundingClientRect() to get the position of the page element
  • javascript quick method to get element position getBoundingClientRect()
  • JavaScript getBoundingClientRect() to get the position of the page element code [revised version]

<<:  MySQL free installation version (zip) installation and configuration detailed tutorial

>>:  How to reset Zabbix password (one-step)

Recommend

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

19 MySQL optimization methods in database management

After MySQL database optimization, not only can t...

First experience of creating text with javascript Three.js

Table of contents Effect Start creating text Firs...

Details of function nesting and closures in js

Table of contents 1. Scope 2. Function return val...

How to center your HTML button

How to center your HTML button itself? This is ea...

How can MySQL effectively prevent database deletion and running away?

Table of contents Safe Mode Settings test 1. Upda...

mysql: [ERROR] unknown option '--skip-grant-tables'

MySQL database reports ERROR 1045 (28000): Access...

MySQL database migration quickly exports and imports large amounts of data

Database migration is a problem we often encounte...

A brief understanding of the three uses of standard SQL update statements

1. Environment: MySQL-5.0.41-win32 Windows XP Pro...

How to automatically back up the mysql database regularly

We all know that data is priceless. If we don’t b...

HTML embed tag usage and attributes detailed explanation

1. Basic grammar Copy code The code is as follows...

Record of the actual process of packaging and deployment of Vue project

Table of contents Preface 1. Preparation - Server...

Example code for implementing triangles and arrows through CSS borders

1. CSS Box Model The box includes: margin, border...

Web componentd component internal event callback and pain point analysis

Table of contents Written in front What exactly i...

MySQL 5.7.13 installation and configuration method graphic tutorial on Mac

MySQL 5.7.13 installation tutorial for Mac, very ...