Skip to main content

Bug Tracker

Side navigation

#5517 closed bug (invalid)

Opened November 16, 2009 07:10AM UTC

Closed December 04, 2010 08:05AM UTC

Last modified July 24, 2013 11:10AM UTC

.offset().top gives incorrect values

Reported by: rene___ Owned by: rene___
Priority: undecided Milestone: 1.4
Component: offset Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:
Description

I use the code below to get the optimum scrolltop position for a comment, so the comment will be scrolled to the center of the window (div).

function getDivScrollTop(Div) {

var Comments = $('#comments');

var Comment = $(Div);

var Top = Comment.offset().top;

var Sibling = Comment.next();

var ViewportHeight = parseInt(Comments.css('height'));

var NewTop = Top - ((ViewportHeight - (Sibling.offset().top-Top)) / 2);

if (NewTop > Top) {

NewTop = Top - (ViewportHeight / 4);

}

return NewTop;

}

But since I changed it from:

Comments[0].offsetTop too Comments.offset().top,

it alternates in right values and then completely wronge values.

I attached a screencapture from IE7's debuggers watches window.

I also tested it with the latest version of Chrome, and the problem is the same.

Attachments (1)
  • offsettop.png (19.4 KB) - added by rene___ November 16, 2009 07:11AM UTC.

    Debuggers watch view of the values monitored, while breaking the getDivScrollTop function

Change History (4)

Changed November 16, 2009 04:10PM UTC by antipop2 comment:1

Another issue which I think is related to offset().top under IE 7&8.

For example - I have an element in an IFrame which I want to position absolutely. When scrolling the whole frame offset().top still returns one and the same position. I've fixed it with adding the scrollTop distance.

//only for IE
function getOffset(element) {
   var offset = $(element).offset();
   var top = offset.top;
   var left = offset.left;
   var isInIFrame = (window.location != window.parent.location) ? true : false;
   if (isInIFrame && $.browser.msie && document.documentElement) {
      if (document.documentElement.scrollTop) {
         top += document.documentElement.scrollTop;
      }
      if (document.documentElement.scrollLeft) {
         left += document.documentElement.scrollLeft;
      }
   }
   return {top:top, left:left};
}

Under FF and Chrome there is no such problem.

Changed November 19, 2010 10:01PM UTC by snover comment:2

owner: brandonrene___
status: newpending

Please provide a reduced test case demonstrating this issue on jsFiddle.

Changed December 04, 2010 08:05AM UTC by trac-o-bot comment:3

resolution: → invalid
status: pendingclosed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!

Changed July 24, 2013 11:10AM UTC by chris.smith@intelliflo.com comment:4

Thanks for your getOffset function above. This worked perfectly for us!