使用 jquery e.pageX 和 e.pageY 调整大小问题
我有一个 div,当您将鼠标悬停在图像上时,该 div 会弹出,只要鼠标悬停在图像上(这是图像的描述),该 div 就会浮动在鼠标上方 20px 处。当我调整窗口大小时,位置会偏离。我的想法是因为它仍在尝试获取旧窗口 e.pageX 和 e.pageY。有没有办法在调整窗口大小时重新计算 e.pageX 和 e.pageY?
下面是计算图像悬停时显示的 div 的代码片段。
mapPieceText.css({left: (e.pageX - (offset.left - 20)), top: (e.pageY - (offset.top + 20))});
I have a div which pops up when you hover over an image, the div floats 20px above the mouse where ever the mouse is over the image (it's a description for the image.) When I resize the window the position gets thrown way off. My thought for this is because it's still trying to get the old window e.pageX and e.pageY. Is there a way so that on resize of the window it recalculates the e.pageX and e.pageY?
Here is the snippet of code that calculates the div that is shown on image hover.
mapPieceText.css({left: (e.pageX - (offset.left - 20)), top: (e.pageY - (offset.top + 20))});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在将“偏移”存储为固定值。您需要重新计算页面调整大小时的偏移量(或使用“dom-live”jQuery 方法)。
It appears you are storing 'offset' as a fixed value. You'll need to recalculate offset on page resize (or use a 'dom-live' jQuery method ).
我没有测试过,但它不应该是
$(this).offset().top
吗?I haven't tested, but shouldn't it be
$(this).offset().top
?