CSS 图像翻转图和 jQuery:如何使用第二个 li 列表?

发布于 2024-09-12 12:20:23 字数 3667 浏览 2 评论 0原文

我目前正在使用这个可点击的地图(狼的地图),但我想使用第二个li 国家名单以及作为脚本基础的国家名单。由于显示原因,第二个菜单不能与地图位于同一 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}
...

I'm currently using this clickable map (Wolf's map) but I would like to use a second li list of countries in addition of the one that serves as a foundation for the script. The second menu cannot be in the same DIV as the map for presentation reasons. I've been searching through the JQuery documentation to understand how I can trigger an action on another element than the one selected but I'm not sure I've got it right - probably not as it is not working.

The original script looks like this :

    $(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);

});

What I would like to do is to trigger the same action when hovering another element that is not #europe li a. I have tried this :

$(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");
 });

});

It makes something but not the desired effect : it looks like the map can move indeed but not to the good positions (some countries get a white background after several hovers on the second menu).

I would be very grateful if you could help me !

Regards.

PS : some relevant HTML and CSS parts.

The original list

<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>
      ...

The menu I would like to add

<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>
...

The original 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}
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-09-19 12:20:24

使用此功能解决的问题:

  $("#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");
   }

Problem solved with this function :

  $("#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");
   }
标点 2024-09-19 12:20:24

据我的理解(猜测),您的问题是,当您将鼠标悬停在 anchor 标记上时,您的地图会变空。我认为您的问题可以通过克隆 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");
 });

缓存您的 new_map 的原因 & new_bg 是如果您要将 append source 元素添加到某个其他元素,则 source 元素将从其原立场

PS:其实我不明白你的问题,我只是猜测上面可能是你的问题,如果你提供了一个在线<强>演示(链接到您的工作页面)。尽管您提供了一些 HTML & CSS 这不足以回答。因为很难猜测你的真实意图是什么。 没有冒犯

根据我的理解(如果它符合你的问题)这个解决方案应该有效

As far as my understanding (Guess), your problem is, when you hover over an anchor tag your map is getting empty. I think your problem can be fixed by cloning the new_map & new_bg before appending to #carteeurope li a

your actual code,

$("#menudroite li a").hover(function(){
    $(new_map).appendTo("#carteeurope li a");
    $(new_bg).appendTo("#carteeurope li a");
 });

I suggest you to do something below,

$("#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");
 });

The reason for caching your new_map & new_bg is If you are going to append the source element to some other element, the source element gets removed from its original position

PS: Actually I don't understand your question, I just guessed the above might be your problem, you could get a good answer from the people around this community if you had provided a online demo (link to ur working page). Though you provided some HTML & CSS it's not enough to answer. Because It is hard to guess what your actual Intention is. NO OFFENSE

According to my understanding (if it matches your question ) this solution should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文