鼠标悬停/鼠标移出、鼠标进入/鼠标离开、悬停闪烁问题
我有一个 div,里面有一个图像。当我将鼠标悬停在图像上时,我会创建一个工具提示 div,该工具提示 div 绝对位于图像的一部分上方(绝对位置很重要)。它包含标题和替代文本。
这一切都很好,直到您将鼠标悬停在工具提示框上。它不会向下冒泡,并且认为我不再将鼠标悬停在图像上,从而使工具提示框消失。但随后它记录我再次将图像悬停在图像上,并且它在显示工具提示框和隐藏它之间来回切换。
因此出现了闪烁问题。
SO 上有很多关于闪烁问题的帖子,我已经尝试了很多解决方案,但没有一个有效。我尝试过 Mouseover/mouseout、mouseenter/mouseleave、hover,甚至将 live() 与它们结合使用。我什至从从头开始创建工具提示切换为在那里使用空 div,这样当页面加载时它就会在 DOM 中,以防出现问题。我真的不知道该怎么办了。这是我现在的代码。
$("img").bind("mouseover", function() {
var pimg = $(this);
var position = pimg.position();
var top = position.top;
var left = position.left;
var title = $(this).attr('title');
var alt = $(this).attr('alt');
$('.toolTip').css({'left' : left, 'top' : top, 'width' : width}).append('<div><h3>' + title + '</h3><p>' + alt + '</p></div>');
});
$("img").bind("mouseout", function() {
$('.toolTip').empty();
});
I have a div with an image inside it. When I hover the image, I create a tooltip div thats absolutely positioned over part of the image (the absolute position is important). It contains the title and alt text.
This is all well and good until you hover the tooltip box. It doesn't bubble down and it thinks I'm no longer hovering over the image, thus making the tooltip box disapear. But then it registers I'm hovering the image again and it goes back and forth between showing the tooltip box and hiding it.
Thus the flickering issue.
There are a bunch of posts on SO about the flickering issue and I've tried so many solutions but none have been working. I've tried Mouseover/mouseout, mouseenter/mouseleave, hover, and even using live() in combination with them. I even switched from creating the tooltip from scratch to having the empty div there so it would be in the DOM when the page loaded in case that was the issue. I really don't know what to do anymore. Here is my code at the moment.
$("img").bind("mouseover", function() {
var pimg = $(this);
var position = pimg.position();
var top = position.top;
var left = position.left;
var title = $(this).attr('title');
var alt = $(this).attr('alt');
$('.toolTip').css({'left' : left, 'top' : top, 'width' : width}).append('<div><h3>' + title + '</h3><p>' + alt + '</p></div>');
});
$("img").bind("mouseout", function() {
$('.toolTip').empty();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 a) 您需要使用 mouseenter / mouseleave 以及 b) 工具提示 div 需要位于具有 mouseenter / mouseleave 侦听器的元素内。
例如:
The problem is a) you need to use mouseenter / mouseleave and b) the tooltip div needs to live inside the element that has the mouseenter / mouseleave listeners.
eg: