【发布时间】:2012-10-05 08:28:45
【问题描述】:
我正在尝试创建地图,当我将鼠标悬停在按钮上时,该地图的某个位置应突出显示。当鼠标进入时,覆盖图像会按原样显示,但是当鼠标移动时,在悬停按钮内,覆盖图像会闪烁,并且会重复触发 mouseEnter 和 mouseLeave 事件。我还尝试了 mouseOver、hover 和 mouseout,结果相同。我做错了吗?
HTML:
<div id="map">
<img src="images/map/map.png" />
<img src="images/map/mapHover.png" id="hoverhighlands" class="hoverButton"/>
<img src="images/map/mapHover.png" id="hovernorth" class="hoverButton"/>
<img src="images/map/mapHover.png" id="hovercentral" class="hoverButton"/>
<img src="images/map/mapHover.png" id="hoverlothian"class="hoverButton" /> <img src="images/map/mapHover.png" id="hoverwest" class="hoverButton" />
<img src="images/map/central.png" class="mapOverlay" id="central" />
<img src="images/map/highlands.png" class="mapOverlay" id="highlands" />
<img src="images/map/lothian.png" class="mapOverlay" id="lothian" />
<img src="images/map/northeast.png" class="mapOverlay" id="north"/>
<img src="images/map/west.png" class="mapOverlay" id="west"/>
</div>
JS:
function setMapOverlays() {
$(".mapOverlay").hide();
$(".hoverButton").mouseenter(
function () {
var id = $(this).attr('id');
id = id.substr(5);
showOverlay(id);
}).mouseleave(function () {
console.log('mouseleave');
var id = $(this).attr('id');
id = id.substr(5);
hideOverlay(id);
})
}
function showOverlay(id) {
$('#' + id).show();
}
function hideOverlay(id) {
$('#' + id).hide();
}
编辑
好的,我知道为什么它不起作用了。当 .show() 函数触发时,我的叠加图像被放置在 hoverButtons 的顶部,触发 onmouseleave。
我设法验证了在 hoverButton 上放置 z-index:2 并在 mapOverlay 上放置 z-index:1。这样当我移动鼠标时不会触发事件。
所以我有一个临时修复。将 onmouseleave 事件移动到我的 mapOverlays 而不是我的 hoverButtons 就可以了。 函数 setMapOverlays() { $(".mapOverlay").hide(); $(".hoverButton").mouseenter( 功能 () { console.log('进入'); var id = $(this).attr('id'); id = id.substr(5);
showOverlay(id);
});
$('.mapOverlay').mouseleave(function () {
console.log('mouseleave');
hideOverlay($(this));
})
}
这可行,但它不是所需的行为。当我的鼠标移出悬停按钮时,我希望覆盖隐藏。关于如何做到这一点的任何建议?
两张图片可以帮助解释我在做什么:
【问题讨论】:
-
尝试在
mouseleave函数后加一个分号。hideOverlay(id); }); -
谢谢,但没有帮助。我想我在叠加层上做错了。也许当我在悬停按钮上显示图像时,它会触发鼠标离开,因为按钮被图像覆盖?
-
javascript 中不需要分号,除非前面的行以 [ 或 (
标签: javascript jquery html mouseevent