Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

javascript - Cross browser issue with offset() jquery function

I am having a cross browser issue with the offset() function in jQuery. For example, I am looking for the offset of an anchor tag

eg. $('#anchorid').offset().top

  • In Firefox 3.6 = 205
  • In IE8 = 204
  • In IE7 = 553

As you can see the difference in each returned value. I am not too concerned with the difference between FF & IE8 but I am with IE7 and the others.

Is there another function I could use that would be the same or similar cross browsers or a possible fix for this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The chances are there is something wrong (non-crossbrowser) with your markup. But as alternative you could try using native javascript instead.

document.getElementById('anchorid').offsetTop

Of if you wanted to get the offset on the whole page you could use a function like:

function findTotalOffset(obj) {
  var ol = ot = 0;
  if (obj.offsetParent) {
    do {
      ol += obj.offsetLeft;
      ot += obj.offsetTop;
    }while (obj = obj.offsetParent);
  }
  return {left : ol, top : ot};
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...