Bug Tracker

Ticket #3742 (closed bug: fixed)

Opened 6 years ago

Last modified 3 years ago

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

comment:1 Changed 6 years ago by flesler

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

comment:2 Changed 6 years ago by brandon

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

This is actually fixed in jQuery 1.3

Note: See TracTickets for help on using tickets.