Firefox and Chrome give different values for offsetTop

后端 未结 8 1935
囚心锁ツ
囚心锁ツ 2021-02-14 14:58

I am trying to position an span element (let us call it \"the tooltip span\") relative to a input field. To do this, I am wrapping the tooltip span and the input field in anothe

8条回答
  •  不思量自难忘°
    2021-02-14 15:29

    I've been having the same problem as you and I realized that (in my case) the thing that was messing up the offset().top value in chrome, was having one or more images without the "height" attribute above the element.

    Before

    
    

    offset.top() was 100 in Chrome

    offset.top() was 150 in Firefox and IE7 (beleive or not, it worked just fine in IE!)

    After

    
    

    offset.top() is 150 in both Firefox, IE7, AND CHROME.

    Notice the the difference was 50px, which is actually the same as the image height.

    Since I was developing a JQuery plugin, i tryed to collect all the images that had their width and height attributes undefined, and manually setting their size with .width() and .height(), but it didn't work, because Chrome returned 0 for both functions. So, i guess the problem with offset.top() actually relies on that. If JQuery is triying to get te offset.top() value by accumulating the "above" element's height, and one of those elements it's an image with no specified height, "0" will be added to that sum, and therefore the top value will be missing those "ignored" heights.

    PS: Sorry for my english, it's been a long time since i wrote such a long text in this language!

提交回复
热议问题