【发布时间】:2011-04-13 01:27:58
【问题描述】:
HTML:
<div class='a'></div>
<div class='b'></div>
CSS:
body {
position: relative;
}
.a {
position: absolute;
left: 100px;
top: 100px;
width: 100px;
height: 100px;
background: #777;
}
.b {
position: absolute;
display: none;
background: red;
}
JavaScript:
$(function() {
$('.a').live('mouseover mouseout', function(e) {
switch (e.type) {
case 'mouseover': {
$('.b').offset({'left': $(this).offset().left,
'top': $(this).offset().top})
.width($(this).outerWidth())
.height($(this).outerHeight())
.show();
break;
}
case 'mouseout': {
$('.b').hide();
break;
}
}
});
});
如您所见here,会发生某种闪烁,因为当显示.b 时,会自动出现mouseout。你会如何解决这个问题?
所需的行为是:当鼠标悬停在.a 上时,应显示.b(应覆盖.a),而当鼠标未悬停在.a 上时,不应显示.b。 .a 应始终显示。
.a 的位置和尺寸不是恒定的(应该即时计算)。
【问题讨论】:
-
最初应该
.b覆盖.a吗? -
没有。仅当鼠标悬停在
.a上时。 -
你可以尝试让
.b成为.a的孩子,尽管有时这可能并不理想 -
您是否尝试过添加几毫秒的计时器,以便它可以触发鼠标悬停事件并保持显示?
-
我不喜欢超时的想法。我正在寻找更清洁的解决方案。
标签: javascript jquery mouseover jquery-events mouseout