P粉3602660952023-08-23 10:35:44
Since this is specifically marked for jQuery -
$("#myElement")[0].getBoundingClientRect();
or
$("#myElement").get(0).getBoundingClientRect();
(The two are functionally the same, in some older browsers, .get()
is slightly faster)
Please note that if you try to get the value via a jQuery call, it will not take into account any css transform values, which may lead to unexpected results...
Note 2: In jQuery 3.0, it has been changed to use the appropriate getBoundingClientRect()
call to make its own size calls (see jQuery Core 3.0 Upgrade Guide) - This means that the other jQuery answers will eventually always be correct - but only when using new jQuery versions - which is why it's called a breaking change...
P粉6499902732023-08-23 00:55:18
You can get the bounding box of any element by calling the getBoundingClientRect method.
var rect = document.getElementById("myElement").getBoundingClientRect();
This will return an object with left, top, width and height fields.