这是一个棘手的问题,我觉得它缺少关于这些图像映射如何工作的知识。反正..
此处可见的问题http://luff.redlinestaging.com/index.php?option=com_content&view=article&id=35
JS代码:
jQuery(document).ready( function() {
jQuery.get('map.php?country=canada', function(output) {
jQuery('#map').html( output );
addClickMap();
});
});
function showState(e) {
//console.log( jQuery(this).is(':hidden' ) );
var title = jQuery(this).attr( 'title' );
var stateClass = '.' + title;
jQuery(stateClass).addClass( 'hover' );
}
function hideState(e) {
//console.log( jQuery(this).is(':hidden' ) );
var title = jQuery(this).attr( 'title' );
var stateClass = '.' + title;
jQuery(stateClass).removeClass( 'hover' );
}
function addClickMap() {
if( jQuery('#canada') ) {
jQuery('#canada area').each( function() {
//jQuery(this).hoverIntent( showState, hideNav );
jQuery(this).mouseenter( showState );
jQuery(this).mouseleave( hideState );
jQuery(this).click( function( e ) {
e.preventDefault();
});
});
}
}
代码:
#map {
width:100%;
}
.map {
width:100%;
height:100%;
position:relative;
}
area {
position:absolute;
top:0px;
left:0px;
z-index: 1001;
display:block;
visibility:visible;
}
.canada {
background-image:url(/map/map_canada.png);
background-repeat:no-repeat;
}
.hover {
display:block !important;
}
.state {
position:absolute;
z-index:2;
top:0px;
left:0px;
display:none;
}
.AB {
top:375px;
left:342px;
}
.BC {
top: 308px;
left: 236px;
}
.SK {
top: 394px;
left: 406px;
}
.NB {
top: 550px;
left: 784px;
}
.MB {
top: 405px;
left: 486px;
}
.NT {
top:198px;
left: 321px;
}
.NL {
top: 380px;
left: 765px;
}
.NU {
top:-4px;
left: 397px;
}
.ON {
top: 456px;
left: 535px;
}
.PE {
top: 532px;
left: 820px;
}
.NS {
top: 551px;
left: 811px;
}
.QC {
top: 364px;
left: 655px;
}
.YT {
top:180px;
left:243px;
}
Generated HTML dump from PHP for Canada:
<div class="map canada">
<img src="/map/map_canada.png" usemap="#canada" class="map_canada"/>
<div class="state AB" style="background-image:url(/map/icons/AB.png); width:107px; height:182px; background-repeat:no-repeat;">
</div>
<div class="state BC" style="background-image:url(/map/icons/BC.png); width:143px; height:239px; background-repeat:no-repeat;">
</div>
<div class="state MB" style="background-image:url(/map/icons/MB.png); width:112px; height:170px; background-repeat:no-repeat;">
</div>
<div class="state NB" style="background-image:url(/map/icons/NB.png); width:56px; height:55px; background-repeat:no-repeat;">
</div>
<div class="state NL" style="background-image:url(/map/icons/NL.png); width:170px; height:164px; background-repeat:no-repeat;">
</div>
<div class="state NT" style="background-image:url(/map/icons/NT.png); width:189px; height:218px; background-repeat:no-repeat;">
</div>
<div class="state NS" style="background-image:url(/map/icons/NS.png); width:83px; height:68px; background-repeat:no-repeat;">
</div>
<div class="state NU" style="background-image:url(/map/icons/NU.png); width:372px; height:425px; background-repeat:no-repeat;">
</div>
<div class="state ON" style="background-image:url(/map/icons/ON.png); width:207px; height:217px; background-repeat:no-repeat;">
</div>
<div class="state PE" style="background-image:url(/map/icons/PE.png); width:39px; height:42px; background-repeat:no-repeat;">
</div>
<div class="state QC" style="background-image:url(/map/icons/QC.png); width:216px; height:259px; background-repeat:no-repeat;">
</div>
<div class="state SK" style="background-image:url(/map/icons/SK.png); width:97px; height:176px; background-repeat:no-repeat;">
</div>
<div class="state YT" style="background-image:url(/map/icons/YT.png); width:118px; height:189px; background-repeat:no-repeat;">
</div>
</div>
<map id="canada" name="canada">
<area shape="poly" coords="410,552,446,401,500,413,485,568," href="#" title="SK" />
<area shape="poly" coords="488,568,503,413,552,419,596,463,545,519,542,572," href="#" alt="" title="MB" />
<area shape="poly" coords="597,466,548,524,546,569,590,589,608,575,641,599,687,629,666,668,700,651,738,619,673,642,691,602,686,589,680,535,655,515,653,485,627,481," href="#" alt="" title="ON" />
</map>
如您所见,它可以很好地悬停鼠标,但将 mouseout 事件调用为 early,或将 mouseleave 事件调用为 early(任您选择,两者都不起作用)。它有时也不是很敏感,但这与我真正关心的问题不同,因为它在鼠标移动时似乎一个接一个地触发 mouseover 和 mouseout 事件。有没有办法让这不那么混乱?
如果无法做到这一点,您认为最好的方法是什么?a) 简化设计并使用 maphilight b) 编写一些非常复杂的函数,根据鼠标位置检测鼠标悬停?将来我们可能希望将自定义图像设计放入每个州/省,因此需要完整的图像显示。