jQuery 在鼠标悬停时将元素放置在彼此旁边 - 修复可滚动 div
所以我摆弄了这个 jsFiddle 的 mouseover 与绝对定位 div
结果是不理想的。该代码基于 jquery 如何相对于另一个元素定位一个元素 但代码没有按预期工作。我可以弄清楚如何根据绝对定位重新定位偏移(例如减去标题的偏移)。但我遇到的问题是滚动定位。一旦开始滚动,位置就错误了。有人知道它的解决方案吗?
So I fiddled with this
jsFiddle of mouseover together with absolute positioning divs
The outcome is undesired. The code is based on jquery how to position one element relative to another
But the code is not working as expected. I can figure out how to reposition the offset depending on the absolute positioning (e.g. substract offset of header). But what I have trouble with is the scrolling positioning. As soon as you start scrolling the position is wrong. Does someone know a solution of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于某种原因,当您滚动文档时,jQuery 中的
offset().top
值会发生变化。相反,只需使用标准 HTML 元素属性offsetLeft
和offsetTop
:工作示例:http://jsfiddle.net/YpcSe/2/
代码:
For some reason the
offset().top
value changes in jQuery when you scroll the document. Instead, simply use the standard HTML element propertiesoffsetLeft
andoffsetTop
:Working example: http://jsfiddle.net/YpcSe/2/
Code:
不想回答我自己的问题,但请参阅这里:
jquery 的工作解决方案
基本上问题是元素插入到了错误的位置。偏移量以某种方式与带有滚动条的页面没有正确关联。这可以通过在父表单上添加元素来解决(例如,如果您想让某些按钮可见)。或者
它也修复了我的重叠元素的问题。想象一下您有一个固定定位的元素 E1 和一个绝对定位的元素 E2。一是左侧菜单,E2是内容。当您想要在将鼠标悬停在 E2 内容中的例如 div 上时使元素可见/显示元素并希望它与左侧菜单 E1 重叠时,那么您需要确保该 div 不在内容中,因为看起来您不能重叠到位置固定的 E2 的同级中。
hate to answer my own questions but see here:
working solution with jquery
basically the problem was that the element was inserted at the wrong location. The offset somehow did not relate correctly to the page with scrollbars. This can be fixed by adding the element on the parent form (in case you wanna make some buttons visible for example). Or the
<body>
It fixes as well a problem with overlapping elements I had. Just imagine you have an fixed positioned element E1 and an absolute positioned element E2. One is the left menu, E2 is the content. When you want to make visible / show an element when you mouseover over a e.g. div in the E2 content and want it to overlap over the left menu E1 then you need to make sure that the div is not in the content, since it seems you cannot overlap into a sibling of E2 which is positioned fixed.