Ticket #3742 (closed bug: fixed)
offset() uses parseInt which rounds down decimal numbers
| Reported by: | ktrott | Owned by: | brandon |
|---|---|---|---|
| Priority: | major | Milestone: | 1.3 |
| Component: | offset | Version: | 1.2.6 |
| Keywords: | Cc: | ktrott | |
| Blocking: | Blocked by: |
Description
In Firefox 3, the value of getBoundingClientRect for top or bottom may be a decimal number, e.g. 602.75 or 602.25.
Firefox appears to render at the actual position of 603 for 602.75. Since jQuery offset method uses parseInt it ends up returning 602. If you are doing exact positioning you end up being off by 1 pixel.
One could argue that jQuery shouldn't make assumptions about how the browser implements the decimal numbers and instead return decimal numbers rather than ints, e.g. return 602.75 rather than 602 or 603. Then when one positions an element at 602.75, the browser would internally handle it.
Here's a workaround we wrote that addressed our use case by patching up the jQuery offset return values:
var offset = $elem.offset(),
elem = $elem.get(0);
if (elem.getBoundingClientRect) {
var rect = elem.getBoundingClientRect(); if (!isNaN(parseInt(rect.top))) {
offset.top += (rect.top - parseInt(rect.top));
} if (!isNaN(parseInt(rect.left))) {
offset.left += (rect.left - parseInt(rect.left));
}
} return offset;
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
