Bug Tracker

Opened 9 years ago

Closed 8 years ago

Last modified 6 years ago

#5517 closed bug (invalid)

.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___ 9 years ago.
Debuggers watch view of the values monitored, while breaking the getDivScrollTop function

Download all attachments as: .zip

Change History (5)

Changed 9 years ago by rene___

Attachment: offsettop.png added

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

comment:1 Changed 9 years ago by antipop2

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.

comment:2 Changed 8 years ago by snover

Owner: changed from brandon to rene___
Status: newpending

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

comment:3 Changed 8 years ago by trac-o-bot

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!

comment:4 Changed 6 years ago by chris.smith@…

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

Note: See TracTickets for help on using tickets.