jQuery offset returns invalid value for <span> or inline <div>

ⅰ亾dé卋堺 提交于 2019-12-05 19:54:12

I think the problem is that as soon as you give the elements "position: absolute", you change a bunch of stuff about the layout. For starters, the <span> elements are just "position: static" and "display: inline". Because they're inline, the "top" value looks big because they're set on a text baseline that's at the bottom of their container. The container <div> is stretched around them, in other words, and the top is the top of the row of text (non-existent in your example, but that's what the browser thinks).

When you flip them to "position: absolute", the meaning of those things changes.

If you start things off with a CSS rule that gives the spans "display: inline-block", then the behavior changes (though it's still somewhat weird). (edit — ah, it's the "text-align: center" that's doing the horizontal movement. Once the first element is yanked out of the line, the other has to re-center itself.)

Note that when you add the spans, giving them a height has no effect; they're inline elements, not blocks.

edit — I've thought about how to do this, but it's a little tricky because it's not clear what effect you want. The problem is that the rules of layout for the inline content will affect the images and cause odd behavior. That "text-align: center", for example, will move things around as soon as you "pin" an image. Thus, with "inline-block", when you move over an element it will freeze in place, but the other image will then be adjusted to obey that "text-align" rule, and thus overlap the pinned one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!