Home  >  Article  >  Web Front-end  >  GetBoundingClientRect usage method and compatibility processing

GetBoundingClientRect usage method and compatibility processing

php中世界最好的语言
php中世界最好的语言Original
2018-04-13 09:35:565281browse

This time I will bring you how to use getBoundingClientRect and compatibility processing. What are the precautions about 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 a html element relative 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 a object.

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); // 元素左边距离页面左边的距离

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

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!

Statement:
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