Bug Tracker

Opened 14 years ago

Closed 14 years ago

Last modified 11 years ago

#4263 closed bug (fixed)

Offset() on position:fixed inside a div incorrect in Webkit browsers

Reported by: BenBernanke Owned by: brandon
Priority: major Milestone: 1.4
Component: offset Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:


I'm using jQuery 1.3.2 and seeing different results on the offset() call when using position:fixed for CSS if the element's inside a div.

A fixed-position button will have it's offset().top value increase as you scroll down the screen. However, if you put a <div> around the floating button, it always returns the same value.

Firefox 3.1 and IE 7 report the expected/correct position for the floating element inside a <div>. Chrome and Safari 3.2 (525.26.13) report the same position every time.

Test case attached.

Attachments (2)

chromefloatOrig.html (857 bytes) - added by BenBernanke 14 years ago.
Position-Fixed Test Case
jquery-1.3.2.js (122.2 KB) - added by joelg 14 years ago.
Modified jquery-1.3.2.js to fix the offset() issue.

Download all attachments as: .zip

Change History (5)

Changed 14 years ago by BenBernanke

Attachment: chromefloatOrig.html added

Position-Fixed Test Case

comment:1 Changed 14 years ago by joelg

I have managed to fix this issue by modifying the core:

Add the following after jquery-1.3.2.js line 4206:

if ( computedStyle.position === "fixed" )

top += Math.max(docElem.scrollTop, body.scrollTop);

left += Math.max(docElem.scrollLeft, body.scrollLeft);

This comes after a line with the following code:

left += parseInt( computedStyle.borderLeftWidth, 10)

And before a line as follows:

prevComputedStyle = computedStyle;

That should do it,

I am aware it is not elegant to be changing the core, is there a better way to make fixes such as this, by extending the core?

I will attach my modified jquery-1.3.2.js (my fixes are lines 4208-4210).

Joel Gascoigne http://twitter.com/joelg87

Changed 14 years ago by joelg

Attachment: jquery-1.3.2.js added

Modified jquery-1.3.2.js to fix the offset() issue.

comment:2 Changed 14 years ago by joelg

Just a quick note, the fix I have posted above may not work as expected if there is more than one parent of the element which has position: fixed. I suggest that it should stop adding to "top" and "left" when it finds the first parent, if this is an issue people come across. Though it seems wrong to me to code an element to have two parents with fixed position.

comment:3 Changed 14 years ago by brandon

Resolution: fixed
Status: newclosed

Fixed in r6284

Note: See TracTickets for help on using tickets.