jQuery:当鼠标不悬停在元素 X 或 Y 上时,隐藏 Y?
我有几张图像并排排列,每当我将鼠标悬停在其中一张图像上时,就会出现该图像的工具提示(带有一些信息等)。工具提示信息是通过 ajax 加载的,但这无关紧要。
问题是,当我将鼠标悬停在其中一张图像时,工具提示就会加载,而每当我将鼠标悬停在其中一张图像之外时,工具提示就会消失。所以我无法将鼠标悬停在工具提示上,而我需要这样做。
所以我想到了记录我在对象中悬停的每个元素的解决方案。像这样:
$('*').mouseenter(function() {
currently_hovering = $(this);
});
然后当我将鼠标悬停在图像之外时,检查鼠标是否悬停在工具提示上,如果是,则不执行任何操作,如果不是,则隐藏工具提示。听起来不错,对吧?但由于某种原因,每当我将鼠标悬停在工具提示上时,它都会拒绝记录。它仍然表现得好像我在图像上一样(但是当我在图像中移动鼠标时,工具提示会闪烁得很厉害)。
我假设它不会记录工具提示,因为它是绝对定位的?它位于图像上方(这样我就可以将鼠标移动到它而不隐藏它)。我尝试过使用 mouseenter/mouseover 和悬停,所有结果都相同。
对此有什么解决办法吗?
I have a few images lined up next to eachother, and whenever i hover one of the images, a tooltip appears for that image (with some information etc). The tooltip infomration is loaded with ajax, but that's irrelevant.
The problem is that the tooltip is loaded when i hover one of the images, and it disappears whenever i hover outside of it. So i am not able to hover over the tooltip, which i need to be able to.
So i thought of the solution to log every element i'm hovering over in an object. Like this:
$('*').mouseenter(function() {
currently_hovering = $(this);
});
and then when i hover out of the image, check if the mouse is hovering over the tooltip, and if it is, then do nothing, if it's not, then hide the tooltip. Sounds good, right? But for some reason it refuses to log whenever i mouse over the tooltip. It still acts as if i am over the image (but the tooltip flashes terribly when i move the mouse around in it).
I am assuming it doesn't log over the tooltip because it is absolutely positioned? It is positioned above the image (that way i'm able to move the mouse to it without hiding the it). I've tried using mouseenter/mouseover and hover, all with the same result.
Are there any solutions to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
部分问题可能是您使用 ajax 加载信息。
您需要在正常页面加载后对添加到页面的数据使用 live() 函数,以便脚本处理所述数据。
有点像这样:
Part of the problem can be the fact that you are loading the information with ajax.
You need to use the live() function on data added to the page after the normal page load for scripts to work on the said data.
Sort of like this:
闪烁是因为当您将鼠标移到工具提示上时,您不再将鼠标悬停在该图像上,并且它将删除它,然后当它被删除时,您突然再次将鼠标悬停在该图像上,因此它会重新出现。
听起来当您将鼠标悬停在工具提示上时,图像的
onmouseout
调用是在工具提示的onmouseover
之前调用的,因此您的currently_hovering
变量是未设置为工具提示。当您将鼠标悬停在图像之外时,您可以尝试使用超时吗?即使 0 秒的超时也可能有效,足以让浏览器在检查
currently_hovering
的值之前处理工具提示已悬停在其上?The flashing will be because when you move the mouse over the tooltip, you're no longer hovering over that image and it's removing it, then when it's removed, you are all of a sudden hovering over the image again so it re-appears.
Sounds like the
onmouseout
call of the image is being called before theonmouseover
of the tooltip when you hover over the tooltip, so yourcurrently_hovering
variable is not set to the tooltip.Could you try using a timeout when you hover out of the image? Even a timeout of 0 seconds might work, just enough to let the browser process that the tooltip has been hovered over before it checks the value of
currently_hovering
?也许您可以将图像包装到
标签中,让 jquery 在该 div 标签悬停时“监听”,并在图像本身之后的该标签内添加工具提示。然后将鼠标悬停在工具提示上不应导致闪烁,因为您“不要离开”导致其可见的 div。 像这样的东西
js 会是这样的
Maybe you can wrap your image into for example
<div>
tag, have jquery "listen" on hover of that div tag and add your tooltip inside this tag right after image itself. Then hovering over tooltip shouldn't cause flickering, because you "dont leave" div that is causing it to be visible. Something likethen js would be something like