GetBoundingClientRect usage method and compatibility processing

php中世界最好的语言
Release: 2018-04-13 09:35:56
Original
5377 people have browsed it

This time I will bring you how to use getBoundingClientRect and compatibility processing. What are theprecautionsabout how to use getBoundingClientRect and compatibility processing. The following is a practical case, let’s take a look.

The role of getBoundingClientRect

getBoundingClientRect is used to obtain the position set of ahtml elementrelative to the view window.

Executing object.getBoundingClientRect(); will get the top, right,bottom, left, width, and height attributes of the element. These attributes are returned as aobject.

getBoundingClientRect()

This method returns a rectangle object containing four properties: left, top, right and bottom. Represents the distance between each side of the element and the top and left sides of the page respectively.

var box=document.getElementById('box'); // 获取元素 alert(box.getBoundingClientRect().top); // 元素上边距离页面上边的距离 alert(box.getBoundingClientRect().right); // 元素右边距离页面左边的距离 alert(box.getBoundingClientRect().bottom); // 元素下边距离页面上边的距离 alert(box.getBoundingClientRect().left); // 元素左边距离页面左边的距离
Copy after login

Browser Compatibility

It can be supported by ie5 and above, but there are a few things that need to be corrected,

IE67's left and top will be 2px less, and there will be no width or height attributes.

Use getBoundingClientRect to write a method to obtain the position set of html elements relative to the window

Copy after login

I believe you have mastered the method after reading the case in this article. Please come for more exciting information. Pay attention to other related articles on php Chinese website!

Recommended reading:

Vue implements the parabola effect of the small ball in the shopping cart Detailed explanation

Detailed explanation of the use of components in Vue.js

The above is the detailed content of GetBoundingClientRect usage method and compatibility processing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!