Using a Second List Element in CSS Image Rollover Map with jQuery

I have been utilizing this interactive map from Wolf's website, but I am interested in incorporating a secondary list of countries alongside the existing one as the base for the script. However, due to presentation reasons, the second menu cannot reside in the same DIV as the map. I have been exploring the JQuery documentation to figure out how to trigger an action on a different element than the one currently selected, but my attempts so far have not been successful.

The current script appears 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 aim to achieve is to trigger the same action when hovering over another element that is not #europe li a. To accomplish this, I tested the following code:

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

});

While this does produce some effect, it doesn't quite achieve the desired outcome - the map seems to move, but not to the correct positions (some countries end up with a white background after multiple hovers on the second menu).

Your assistance in resolving this issue would be greatly appreciated!

Kind regards.

PS: Here are some relevant HTML and CSS snippets.

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 additional menu

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

Answer №1

Solution found using the following 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");
   }

Answer №2

From what I gather, it seems like the issue you're facing is that when you hover over an anchor tag, your map disappears. One potential solution could be to clone both the new_map and new_bg before appending them to #carteeurope li a

Your current code snippet,

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

I recommend trying something like this instead,

$("#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 purpose of caching the new_map and new_bg is because when you append an element to another one, the original element is removed from its initial position.

Note: I must admit that I may not fully understand your question and my response is based on assumptions. It would be helpful if you could provide a live demo (a link to your working page) for better assistance. Simply sharing HTML and CSS snippets might not convey your exact intentions clearly. No hard feelings

In case my interpretation aligns with your query, this proposed solution should address the issue at hand

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can I take to make sure that the content on my website is properly sized to fit within the browser window?

I'm currently working on a website project for my college assignment and I could really use some assistance with a significant issue that I've encountered. Whenever I open the html file of my website on my MacBook, which has a screen resolution ...

Initiating a conversation from deep within a conversation using JQuery Mobile

I've been attempting to open a Dialog from within another Dialog, but so far no success. Take a look at the HTML code below: <a href="#popupDialog" data-rel="popup" data-position-to="window" data-role="button" data-inline="true" data-transition=" ...

Preventing the page from scrolling to the top while utilizing an Angular-bootstrap modal and a window position:fixed workaround on an iPad

It's common knowledge that there is a bug in bootstrap modals on certain devices, causing the page behind the modal to scroll instead of the modal itself (http://getbootstrap.com/getting-started/#support-fixed-position-keyboards) An easy fix for this ...

Notify the user with a Jqgrid alert when they attempt to select multiple checkboxes

When editing rows, I wanted to avoid multiple selections. In order to achieve this, I need to check the condition if(count>1) and display an alert message. I am struggling to figure out how to retrieve the count of selected checkboxes in jqGrid. var m ...

Trying to align two divs side by side with the first one floating left and the second one fixed in position? Unfortunately

Here is an example that I have: https://jsfiddle.net/2z2ksLy4/ HTML: <div class="one"> test </div> <div class="two"> test2 </div> CSS: .one{ width:400px; border:1px solid red; min-height: 200px; float:left; display: ...

Is it possible to use jQuery to create a slide-up effect from the

Could I reverse the animation direction of my dropdown menu? Currently, it expands from the top to bottom. Is there a way to make it expand from the bottom to the top? Code snippet: animate({height:'show',opacity:'show'}, ddsmoothmenu ...

ajax used to retrieve xml data and process it

I've been working on retrieving information from an API (specifically BBB) and the response I'm getting looks like this: <?xml version="1.0"?> <response> <returncode>SUCCESS</returncode> <meetingID>ldapreade ...

Set $scope.model childs to an empty string or a null value

While working on a form, I am using a $http request to send the information through Angular in the following manner: $http({ method:"POST", url: "controllers/servicerequest.php", data: { servicerequest: $scope. ...

The table does not move up or down when using the mousewheel over the rows, but only when

I'm encountering an issue with a table inside a div that has overflow-y: scroll applied to it. While the scrollbar appears when there is content overflow, I am unable to scroll using the scrollwheel. It only works when the cursor is directly on top o ...

Creative ways to position and underline text using CSS

I'm having difficulty with positioning the text and extending the underline to the edge of the screen in order to create a specific visual effect. .header { height: 550px; width: 100%; background-color: #1F2041; position: relative; } . ...

Align three out of seven items in the navbar to the right using Bootstrap 4.1

Bootstrap 4.1 offers a great way to create a Navbar with multiple menu items, but I'm struggling to right justify three specific menu items within the Navbar. I've tried different methods, including using align-self-end, justify-content-end, and ...

Adjust the color of a contenteditable div once the value matches

I currently have a table with some contenteditable divs: <div contenteditable="true" class="change"> This particular JavaScript code is responsible for changing the color of these divs based on their content when the page loads. However, I am now ...

The issue of border-radius and shadow box inconsistency

I'm trying to style a div with the following properties: .example { margin: 200px; width: 200px; height: 200px; border-radius: 20px; box-shadow: white 0px 0px 0pt 8pt, #033354 0px 0px 0pt 11pt; } <div class="example"> </div> ...

What could be causing my image not to show up on ReactJS?

I'm new to ReactJS and I am trying to display a simple image on my practice web app, but it's not showing up. I thought my code was correct, but apparently not. Below is the content of my index.html file: <!DOCTYPE html> <html> & ...

Guide to implementing a variable delay or sleep function in JQuery within a for loop

I've noticed that many people have asked similar questions, but none of the solutions provided seem to work in my specific case. My goal is to incorporate a delay within a for loop with varying time lengths. These time lengths are retrieved from an a ...

Troubleshooting: EJ Syncfusion React Scheduler Issues

I have recently implemented a syncfusion calendar for managing appointments in my ReactJs project, following the code examples from this link . After editing the JS and CSS codes (available at ), I encountered an issue with the CSS not displaying correctl ...

Creating PDF files with a generic handler (ASHX)

Recently, I was tasked with generating PDFs on the fly using data stored in a database for my application which utilizes HTML, jQuery and WCF. I ran into some roadblocks when trying to achieve this using client-side technologies like jQuery or plugins. Al ...

Animating background color with jQuery when the page loads

I am facing a challenge that seems simple yet tricky - I want to apply my hover effect on page load. My navigation system consists of div boxes, with jQuery code to animate their background color on hover: <script type="text/javascript" src="http://jq ...

Using jQuery, stop any click or touchstart events on child elements of the target element

Below is my current jQuery code: $(document).on('click touchstart', function (e) { if ( (!$(e.target).hasClass('sb')) ) { $('#features .sb .screen').animate({ top: '6.438em' }, 150); } } I want to twea ...

What is causing Firefox to display an additional line in this section?

After much effort, I've been attempting to eliminate the extra spacing on this page in Firefox: Do you see the menu? If you compare it to Chrome, you'll notice that the menu is positioned too low beneath the background, giving the layout a broke ...