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