[英]CSS image rollover map and jQuery : how to use a second li list?
我目前正在使用此可点击的地图( Wolf的地图 ),但是除了用作脚本基础的国家之外,我还要使用第二个国家列表。 由于演示的原因,第二个菜单不能与地图位于同一DIV中。 我一直在搜索JQuery文档,以了解如何触发对选定元素以外的其他元素的操作,但是我不确定我是否正确-可能无法正常工作。
原始脚本如下所示:
$(document).ready(function(){
var new_map='<span class="map"><span class="s1" /><span class="s2" /><span class="s3" /><span class="s4" /><span class="s5" /><span class="s6" /><span class="s7" /><span class="s8" /><span class="s9" /><span class="s10" /><span class="s11" /><span class="s12" /><span class="s13" /><span class="s14" /><span class="s15" /><span class="s16" /><span class="s17" /><span class="s18" /></span>';
var new_bg=$("<span>");
new_bg.addClass("bg");
$("#europe li a").append(new_map);
$("#europe li a").append(new_bg);
});
我想做的是当悬停另一个不是#europe li a的元素时触发相同的动作。 我已经试过了:
$(document).ready(function(){
var new_map='<span class="map"><span class="s1" /><span class="s2" /><span class="s3" /><span class="s4" /><span class="s5" /><span class="s6" /><span class="s7" /><span class="s8" /><span class="s9" /><span class="s10" /><span class="s11" /><span class="s12" /><span class="s13" /><span class="s14" /><span class="s15" /><span class="s16" /><span class="s17" /><span class="s18" /></span>';
var new_bg=$("<span>");
new_bg.addClass("bg");
$("#carteeurope li a").append(new_map);
$("#carteeurope li a").append(new_bg);
$("#menudroite li a").hover(function(){
$(new_map).appendTo("#carteeurope li a");
$(new_bg).appendTo("#carteeurope li a");
});
});
它会产生某些效果,但效果并不理想:看起来地图确实可以移动但不能移动到正确位置(某些国家在第二个菜单上悬停了几下后才变成白色背景)。
如果您能帮助我,我将不胜感激!
问候。
PS:一些相关的HTML和CSS部分。
原始清单
<div id="b">
<ul id="europe" class="bottom five_columns">
<li id="europe1"><a href="#" title="Albania">Albania</a></li>
<li id="europe2"><a href="#" title="Andorra">Andorra</a></li>
<li id="europe3"><a href="#" title="Austria">Austria</a></li>
...
我要添加的菜单
<div id="menudroite">
<ul>
<li><a href="#">Accueil</a></li>
<li><a href="#" title="France">France</a></li>
<li><a href="#" title="Grèce">Grèce</a></li>
...
原始CSS
#europe,#europe span.bg{background:transparent url('europe-600px.png') no-repeat -9999px 0}
#europe{position:relative;top:0;left:0;display:block;background-position:0 -966px;list-style:none}
#europe *{padding:0;margin:0;border:0 none;outline:0 none}
#europe li{cursor:pointer}
#europe li span{position:absolute;display:block;width:0;height:0;z-index:15}
#europe li a span.bg{z-index:3}
#europe li .map{top:0;left:0}
...
#europe li span{position:absolute;display:block;top:0;left:0;width:0;height:0;z-index:15}
#europe1 a:hover .bg{top:395px;left:303px;width:20px;height:40px;background-position:-10px -10px} #europe1 .s1{top:401px;left:303px;width:15px;height:32px} #europe1 .s2{top:397px;left:305px;width:8px;height:4px} #europe1 .s3{top:418px;left:318px;width:4px;height:9px}
#europe2 a:hover .bg{top:385px;left:133px;width:5px;height:6px;background-position:-35px -10px} #europe2 .s1{top:383px;left:131px;width:9px;height:10px}
...
使用此功能解决的问题:
$("#menudroite li.menudroitepays a").hover(
function(){
var country=$(this).attr("id");
$("#europe" + country + " a").addClass("active");
},
function(){
var country=$(this).attr("id");
$("#europe" + country + " a").removeClass("active");
}
据我的理解( Guess ),您的问题是,当您将鼠标悬停在一个anchor
标签上时,您的地图将为空。 我认为您的问题可以通过在附加到#carteeurope li a
之前克隆new_map
和new_bg
来#carteeurope li a
您的实际代码,
$("#menudroite li a").hover(function(){
$(new_map).appendTo("#carteeurope li a");
$(new_bg).appendTo("#carteeurope li a");
});
我建议你做下面的事情,
$("#menudroite li a").hover(function(){
var cache_new_map = new_map; //cache the new_map
var cache_new_bg = new_bg; //cache your span
$(cache_new_map).appendTo("#carteeurope li a");
$(cache_new_bg).appendTo("#carteeurope li a");
});
caching
new_map
和new_bg
的原因是:如果要将源元素附加到其他元素,则源元素将从其原始位置中删除。
PS :其实我不明白您的问题,我只是猜测以上可能是您的问题,如果您提供了在线演示 (链接到您的工作页面),则可以从社区中的人们那里得到很好的答案。 尽管您提供了一些HTML和CSS,但仍不足以回答。 因为很难猜测您的实际意图是什么。 NO OFFENSE
根据我的理解(如果它与您的问题匹配),此解决方案应该有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.