Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari's getBoundingClientRect() Implementation Returns Incorrect Top

I have a div with float: right and I'm trying to get its distance from the top of the viewport. In every browser except Safari I'm getting the expected results from element.getBoundingClientRect().top. Safari appears to be reporting the element's distance from the top of the page rather than the viewport. This is contrary to what is defined in the spec for this method (as I understand it). Is Safari just not following the rules here, or are other browsers being more forgiving of some mistake of mine that I'm not seeing?

like image 422
Jackson Egan Avatar asked Oct 13 '14 19:10

Jackson Egan


People also ask

Why is getBoundingClientRect not working?

The "getBoundingClientRect is not a function" error occurs for multiple reasons: calling the getBoundingClientRect() method on a value that is not a DOM element. placing the JS script tag above the code that declares the DOM elements. misspelling getBoundingClientRect (it's case sensitive).

What is getBoundingClientRect () Top?

getBoundingClientRect() method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.

Does getBoundingClientRect include padding?

Border, padding and margin are not included.


1 Answers

This appears to be a bug that effects the way that Safari reports element and scroll locations on its load event. If you run the exact same call with a 500ms timeout everything works as expected.

I will update when I hear back on my bug report.

like image 114
Jackson Egan Avatar answered Oct 16 '22 22:10

Jackson Egan