考虑以下代码:
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;
}
}
});
});
正如您在此处看到的,会发生某种闪烁,因为当.b
显示时,mouseout
会自动发生。你将如何解决这个问题?
期望的行为是:当鼠标悬停时.a
,.b
应该显示(应该覆盖.a
),当鼠标未悬停时.a
,.b
不应该显示。.a
应始终显示。
的位置和尺寸.a
不是恒定的(应即时计算)。