Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#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
Blocked by: Blocking:

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 (2)

comment:1 Changed 8 years ago by flesler

  • Cc ktrott added
  • Component changed from unfilled to offset
  • Owner changed from flesler to brandon

comment:2 Changed 8 years ago by brandon

  • Resolution set to fixed
  • Status changed from new to closed

This is actually fixed in jQuery 1.3

Note: See TracTickets for help on using tickets.