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

Introduction to ufw firewall in Linux

Let's take a look at ufw (Uncomplicated Firew...

Practical example of nested routes in vue.js Router

Table of contents Preface Setting up with Vue CLI...

CentOS6.9+Mysql5.7.18 source code installation detailed tutorial

CentOS6.9+Mysql5.7.18 source code installation, t...

Thirty HTML coding guidelines for beginners

1. Always close HTML tags In the source code of p...

Analysis of Context application scenarios in React

Context definition and purpose Context provides a...

Solve the problem of ifconfig being unavailable in docker

Recently, when I was learning docker, I found tha...

Vue3 list interface data display details

Table of contents 1. List interface display examp...

SQL implementation of LeetCode (184. The highest salary in the department)

[LeetCode] 184. Department Highest Salary The Emp...

How to add Vite support to old Vue projects

1. Introduction I have taken over a project of th...

Specific use of MySQL binlog_ignore_db parameter

Preface: After studying the previous article, we ...