Thursday, October 04, 2007

Problem with offsetTop and element position location in IE

This is just a small note for someone looking into a similar problem.

I was implementing keyboard navigation with explicit scrolling using scrollTo method, and found out that in IE I cannot get correct values for element position on the page. I use Prototype and it's Position.cumulativeOffset method (which, in turn, uses offetTop/offsetParent properties of the element). Position calculation worked fine in FF, Safari and Opera, but sometimes failed in IE.

The DOM tree I was operating with consisted of several nested UL, LI, SPAN elements (SPAN inside LI).

I added some debug code and found out, that SPAN elements sometimes got huge offset relative to LI, though it should be simply 0 (FF showed value of -1).

The solution to the problem was to add position:relative style to the SPAN, after that the problem disappears.

Unfortunately, I was unable to reproduce the problem with a simple test case to give the example here.



Labels: ,

8 Comments:

At 7:25 PM , Anonymous Raphael said...

Nice, thanks, that helped me a lot (I had a similar problem in IE).

 
At 7:25 PM , Anonymous Anonymous said...

It's working for me too. Thanks!

 
At 8:44 PM , Anonymous Mark said...

Solved a big headache for me too! thanks

 
At 6:58 PM , Blogger jgroveuk said...

Thank you! After much fruitless googling this finally sorted a problem I was having with IE6. I had to add some dummy 'position:relative' style to some TD elements in order to get things to work correctly.

 
At 8:19 AM , Blogger Girish Kumar said...

Thanks a lot man it is working

 
At 1:04 PM , Anonymous Anonymous said...

Yes, it helped. Thanks.

 
At 7:31 PM , Anonymous Anonymous said...

Great tip.
Reason 923 to not use Internet Exploder.

 
At 3:19 PM , Blogger maca` said...

Working for me too!
IE has many bugs - no surprise there.

 

Post a Comment

Links to this post:

Create a Link

<< Home