Show the appropriate tooltip message based on the circle that I hover over

I have a collection of 5 clickable circles, each with a unique ID and its own tooltip text that is sourced from an xml Data Interface.

    jQuery(function ($) {
    $.ajax({
        type: "GET",
        url: 'steel_agg_bof_flash_en.xml',
        dataType: "xml",
        success: xmlParser
      });
      function xmlParser(xml) {

          $(xml).find("hotspot").each(function () {
            var position   =  $(this).find('position').text();
            var arr        =  position.split(",");
            var hotspotid  =  $(this).find('hsid').text();
            var title      =  $(this).find('title').text();

            $('#clickAreas').prepend('<div id="'+ hotspotid +'_clickable" class="circle" onclick="changeStyle(id);" style="background: #004593; position: absolute; top: ' + arr[1] + 'px' + '; left: ' + arr[0] + 'px' +'; width: ' + Math.floor(arr[2]/3.28148) + 'px; height: '+ Math.floor(arr[2]/3.28148) + 'px; border-radius: 100%; cursor: pointer; opacity: 0.5;"><div class="tooltip"><p style="color: #ffffff;"> ' + title + '</p></div></div>');
          });

        } 

    });

The resulting HTML output can be seen at this link - http://jsfiddle.net/HJf8q/1860/

Later on, I added some CSS and Javascript to achieve the following:

  • When hovering over a circle, the tooltip should fade in, and when moving the mouse out of the circle, the tooltip should fade out (Check out the issue I faced with a single circle here - http://jsfiddle.net/HJf8q/1851/),
  • I'm having trouble displaying the corresponding tooltip text for each circle individually when hovered over, as it currently affects all circles at once. Any help or guidance would be greatly appreciated.

Answer №1

To achieve the fade effect in a tooltip, you can utilize CSS transition along with adjusting the opacity of the tooltip. Below is a concise example demonstrating how this can be implemented:

var tooltips = document.querySelectorAll('.clickMapItem .tooltip');

var items = document.querySelectorAll('.clickMapItem').forEach(function(el) {
    el.addEventListener('mousemove', function(e) {
        if (e.target == this) {
            var tooltip = e.target.querySelectorAll('.tooltip')[0];
            tooltip.style.opacity = 1;

            if (typeof tooltip !== "undefined") {
                var x = (e.clientX - 10) + 'px',
                    y = (e.clientY - 70) + 'px';

                tooltip.style.top = y;
                tooltip.style.left = x;
            }
        }
    });

    el.addEventListener('mouseout', function(e) {
        if (e.target == this) {
            var tooltip = e.target.querySelectorAll('.tooltip')[0];
            tooltip.style.opacity = 0;
        }
    });
});
body {
  background: grey;
}

.clickMapItem {
    background: #004593;
    /*position: absolute;
    top: 100px;
    left: 100px;*/
    width: 135px;
    height: 135px;
    border-radius: 100%;
    cursor: pointer;
    opacity: 0.5; 
    transition: all 0.5s ease;    
    margin: 30px;
}

.clickMapItem:hover {
    background: #004593;
    opacity: 0.8;

}
.clickMapItem .tooltip {
    /*display:none;*/
    opacity: 0;
    display:block;
    position:fixed;
    overflow:hidden;
    background: black;
    color: white; 
    transition: opacity .25s ease-in-out;
    -moz-transition: opacity .25s ease-in-out;
    -webkit-transition: opacity .25s ease-in-out;
   
}
/*.clickMapItem:hover .tooltip {
    opacity: 1;
}*/

/*.clickMapItem:hover .tooltip:before {
  content: ' ';
  position: absolute;
  width: 0px;
  height: 0px;
  left: 38px;
  top: 10px;
  border: 15px solid;
  border-color: #fff transparent transparent #fff;
}*/
<div id="clickMapItem_1" class="clickMapItem">
    <div class="tooltip">
        <p>Tooltip Text</p>
    </div>
</div>


<div id="clickMapItem_2" class="clickMapItem">
    <div class="tooltip">
        <p>One more ytoolytip here</p>
    </div>
</div>

It is recommended to attach event listeners directly to elements instead of relying on global window events for specific element tasks.

UPDATE:

To address the issue of the tooltip staying visible when the mouse hovers over it, utilize javascript events like mousemove and mouseout for comprehensive hover handling. This approach allows filtering out child elements that might trigger unwanted events. Test the updated code provided to ensure it functions as desired.

Answer №2

Hey there, I was able to figure it out using this method

Check out the code here: jsfiddle.net/HJf8q/1868/

I still need to make a few adjustments though, like adding opacity to the circle when hovering and implementing a transition on mouse movement

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

Aligning two divs vertically within another div without relying on the display:flex property

My goal is to align the content of two divs to the top within a parent div. I am unable to use display: flexbox, and it needs to be implemented as inline CSS. Currently, the layout appears as intended when the description div has only one line. However, i ...

What are some ways to expand the width of a MaterialUI form control if no value has been chosen?

I am currently working on a project where I need a dropdown menu component with specific selections. However, the layout appears to be cramped and I'm struggling to adjust the width. Additionally, I've been unsuccessful in changing the font size ...

I'm puzzled as to why the color of my top border isn't displaying even though I've correctly implemented the border

Struggling to make the top border of my div change color. I've double-checked the syntax, and it all looks correct. border: 1px solid white; border-top-color: red; ...

Tips for transferring a reference to a variable, rather than its value, to a method in VueJS

Within my template, there is this code snippet: <input @input="myMethod(myVariableName)" /> Following that, I have the myMethod function: myMethod(variablePassed) { console.log(variablePassed) } Upon execution, I receive the value of th ...

Ways to customize tab display in Bootstrap 5

I am encountering an issue with my code that was previously functioning correctly in Bootstrap 3.3: $('#rides-tab').tab('show'); After upgrading to Bootstrap 5, I am now receiving an error ('tab' is not a function). I have at ...

tips for selecting various API requests based on the selected drop down menu choice

Hey there! I'm looking to enhance my login page by adding a feature that allows users to select from a dropdown menu with different options. Each option will be linked to a specific API, and based on the API response, the user's ability to log in ...

The displayed database results will appear in the opposite order of what was originally assigned for both inline and block elements

I have a database with information that I am trying to display using a while loop. My goal is to show the results in this format... Firstname Lastname - Firstname Lastname - Firstname Lastname player1 ---------------player1-----------------------pla ...

GLTF file: Error 404 - File could not be located

Working on loading a GLTF file and encountering a specific error: https://i.sstatic.net/EbovJ.png Curious as to why the file cannot be located and opened. Is it necessary to establish a local server for this process? After reviewing other examples online ...

Implementing Vuetify 3 and vue-i18n for the tag attribute

When looking to include localized text within tag content, I typically use the following method: <span>{{ $t('myText') }}</span> However, I have been unable to find a way to add localized text for tag attributes. I attempted the foll ...

React and React Native not synchronizing with authentication context

It seems like there is an issue with the AuthContext not updating properly. Despite logging at various points, the user is still not being set. Here's a glimpse of the code in question: App.tsx export default function App() { const { user, setUser ...

Mobile Device Centering Problem with Bootstrap Cards

Despite my thorough research efforts, I have been unable to find a solution. The code snippet below is meant to display the image provided, but it seems to shift to the left when viewed on a mobile device. If anyone can assist me with this issue, I would ...

Can you explain the distinction between sockets and proxy passing in nginx compared to uwsgi?

My latest project setup involves flask, uwsgi, and nginx. The backend solely serves json data through an API, while the client side takes care of rendering. Typically, my Nginx configuration looks like this: My usual setup (with basic proxy passing): se ...

If you invoke functionA within functionB, will functionA be called? How does this affect the compilation process?

I've been diving into the "You Don't Know JS" book series and came across a confusing piece of code. While looking at the following code snippet, I noticed that when I tried running it, nothing was printed out. Despite having "foo()" inside the f ...

What could be the reason behind Firefox displaying 0 as the value retrieved from $(window).height()?

It appears that Firefox is having trouble returning the correct value when using $(window).height(). I am currently using jQuery version 1.8.2. Could this be a bug specific to that version? Other browsers I have tested seem to return the correct viewport ...

Node.js continually throwing 503 errors

After successfully installing and running Node.js for a while, I attempted to execute a standard Hello World program. The code snippet looked something like this: var http = require("http"); http.createServer(function (request, response) { response.w ...

Creating a unique 32-bit unsigned integer identifier for use in Couchbase

I am in search of a method to create a distinct ID for a nosql database. Unlike traditional relational databases, there are no rows to increment from. Typically, UUID's are used to address this issue. However, I require an additional ID (apart from t ...

Switching an element from li to div and vice versa using Jquery Drag and Drop

I am currently experimenting with the following: Stage 1: Making the element draggable from li to div upon dropping into #canvas Placing the draggable element inside #canvas Stage 2: Converting the draggable element from div back to li when dropped i ...

Troubleshooting: Layout display issue on Internet Explorer 8

The first image is from IE9 and above, while the second one is from IE8. In IE8, the right side goes down because it's displaying in mobile view (Check the top menu) Here is the header code I am using: <meta charset="utf-8"> <meta h ...

When a new element is dynamically added to a table cell, how is the computation of "width: 100%" handled?

When utilizing Backgrid for editing values in a table, an <input type="text"> is inserted inside the <td>. Despite setting the input to max-width: 100%, it can still cause the column to expand beyond desired dimensions. To see this behavior in ...

Adjust the height of the bootstrap carousel

I'm struggling with adjusting the height of my bootstrap carousel. Altering the width of the carousel div impacts the size, but I want to maintain full width while changing the height so that only half of the image is visible and centered within the e ...