"Tooltip that flickers when hovering over it with the mouse

Whenever I hover over an SVG rectangle, my tooltip starts flickering erratically. The display switches between "none" and "flex" unpredictably.

Interestingly, when I tested the same code with a div element instead of a rectangle, the result was much smoother. It appears to be something specific to the rect element causing this issue.

In the snippet below, I noticed that the behavior is slightly different compared to when I view it in Chrome. In the snippet, it works fine when entering the rect from left to right or top to bottom, but not the other way around. However, in Chrome, it just keeps flickering regardless of the entry direction.

I'm curious about why it's acting this way and what steps I can take to address it?

const tooltips = document.querySelectorAll('.tooltip');
                const bars = document.querySelectorAll('rect');

                document.addEventListener('mousemove', fn);

                function fn(e) {
                    tooltips[0].style.left = e.pageX + 'px';
                    tooltips[0].style.top = e.pageY + 'px';
                }

                bars[0].onmouseenter = () => {
                    tooltips[0].style.display = 'flex';
                }
                bars[0].onmouseleave = () => {
                    tooltips[0].style.display = 'none';
                }
.tooltip {
  display: none;
  position: absolute;
  background-color: grey;
}
<div class="tooltip">
            <p>Rect 1</p>
            </div>

            <svg width="500" height="500">
                <rect width="200" height="30" x="0" y="0" style="fill:rgb(0,0,255)" />
            </svg>

Answer №1

When the tooltip pops up, it overlaps with the mouse cursor. This triggers a `mouseleave` event for the rectangle, causing the tooltip to close and another `mouseenter` event to trigger again, creating an infinite loop. To prevent this, we can use `pointer-events: none` to disable mouse events on the tooltip.

const tooltips = document.querySelectorAll('.tooltip');
const bars = document.querySelectorAll('rect');

document.addEventListener('mousemove', fn);

function fn(e) {
  tooltips[0].style.left = e.pageX + 'px';
  tooltips[0].style.top = e.pageY + 'px';
}

bars[0].onmouseenter = () => {
  tooltips[0].style.display = 'flex';
}
bars[0].onmouseleave = () => {
  tooltips[0].style.display = 'none';
}
.tooltip {
  display: none;
  position: absolute;
  background-color: grey;
  pointer-events: none;
}
<div class="tooltip">
  <p>Rect 1</p>
</div>

<svg width="500" height="500">
    <rect width="200" height="30" x="0" y="0" style="fill:rgb(0,0,255)" />
</svg>

Alternatively, you can move the tooltip slightly away from the mouse by using `e.pageX + 5` and `e.pageY + 5`, as shown in the example.

const tooltips = document.querySelectorAll('.tooltip');
const bars = document.querySelectorAll('rect');

document.addEventListener('mousemove', fn);

function fn(e) {
  tooltips[0].style.left = (e.pageX + 5) + 'px';
  tooltips[0].style.top = (e.pageY + 5) + 'px';
}

bars[0].onmouseenter = () => {
  tooltips[0].style.display = 'flex';
}
bars[0].onmouseleave = () => {
  tooltips[0].style.display = 'none';
}
.tooltip {
  display: none;
  position: absolute;
  background-color: grey;
}
<div class="tooltip">
  <p>Rect 1</p>
</div>

<svg width="500" height="500">
    <rect width="200" height="30" x="0" y="0" style="fill:rgb(0,0,255)" />
</svg>

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

The Angular controller fails to display any data when inserted within double curly braces on the HTML page

I have been working on a basic binding application using TypeScript. I created a controller called 'bugCtrl' and it appears to be functioning correctly in debug mode (with console.log and alert statements). Below is the HTML code from my page: & ...

My Next.js application does not display an error message when the input field is not valid

I've recently developed a currency converter application. It functions properly when a number is provided as input. However, in cases where the user enters anything other than a number or leaves the input field empty, the app fails to respond. I aim t ...

Validation of a multi-step form ensures that each step is filled out

I'm completely new to Angular and I'm attempting to implement validation in my form. The form structure was not created by me and is as follows: <div> <div ng-switch="step"> <div ng-switch-when="1"> < ...

The functionality of BASE64 encoding in HTML appears to be malfunctioning

I have exhausted all options in my attempt to display an image using a base64 string, but none of them seem to be working. I have tested it on IE6,7, Firefox 3. Can someone please point out what is wrong with the code below? <head> <STYLE type ...

Activate the drop-down division when the search box is in focus with xoxco technology

Currently, I am incorporating Xoxco's tag input plugin which can be found at the following link: In my customization, I have implemented JQuery's focus() function <input id="tags_1" class="tag-holder" type="text" class="tags" /></p> ...

Avoid creating a line break directly after the header, however allow for a line break

I find myself in a scenario where I require a paragraph that has a specific format: An Emphasized Title Some relevant information regarding the subject... I am looking to insert a new line before the title, ensuring it seamlessly integrates with the re ...

Glowing semi-opaque about spotify?

Recently, I decided to challenge myself by recreating the Spotify homepage using only pure Javascript and SCSS as a way to test my front-end development skills. You can view my progress so far at this link, although please note that it's still a work ...

Tips for retrieving date format specific to a particular locale

Exploring Angular features has been an exciting learning journey for me, especially when it comes to handling date time formats. I have successfully configured the user locale using moment.js on a global scale and utilized moment.js to display the date/t ...

Ensure that the page header remains in place at the top of the screen as

I'm interested in maintaining the highlighted components fixed at the top of the page to avoid overlapping with the text below during scrolling. Explore more ideas here Is there a specific code I can include in my "custom-msmbstyle.css" file to achi ...

Utilizing Smarty to Retrieve JavaScript Variables from Database Entries

Currently, I am working on a PrestaShop page that uses the file extension ".tpl". To enable auto complete for the javascript code, I have defined an array of currencies as shown below: var currencies = [ { value: 'Afghan afghani', data: 'AF ...

Selecting Items in React-Native

I am looking to implement a feature where I can select an item from a list and highlight it. I have a function that deletes a specific item from the array, and I would like to repurpose this function to achieve the highlighting effect. The data consists o ...

The issue with the jQuery Mobile onclick event is that it fails to remove the ui-disabled

Ever since I delved into coding with jQuery Mobile, I've noticed a change in the function that once effortlessly removed the "ui-disabled" class. Now, all it does is display an alert message... Snippet from the HTML code: <div data-role="navbar ...

What is the best way to retrieve form input and select values using Angular Binding?

Just starting out with angular2, so please be patient with me. I realize this might be a beginner's question for some. <form> <label class="input-group"> <p>View By</p> <select [(ngModel)]="balance.viewBy" name="v ...

Each time an ajax request is made, the mCS_no_scrollbar class is consistently applied

I recently integrated the jquery CustomScrollbar plugin into my project. You can find more information about this plugin at the following link: Currently, I am loading data dynamically using ajax. Below is a snippet of the code I'm using: $(window). ...

Can you stop a JavaScript event using another event?

Can you prevent a JavaScript event in the queue from being executed by another event? Situation: I have two ASP.Net controls, Age - TextBox Save - Button There are two JavaScript validation functions, ValidateAge() - verifies if the age is between ...

The name 'Landbot' cannot be located. Have you meant to type '_landbot' instead?

I'm currently in the process of integrating Landbot into my React.js application with TypeScript. I'm following this [doc] 1. However, I'm facing an issue where the code inside useEffect (new Landbot.Container) is causing an error. 'C ...

What is the best way to eliminate the "onclick" attribute from an HTML element using JavaScript?

Is there a way to make my code only execute the onlick function after a button is pressed? I want to prevent any action from happening before that. <!-- deactivate onclick function --> <div class="boardsection2" id="2_8_7" oncl ...

Positioning elements to the right inside a Bootstrap 4 thumbnail behaves in a unique way when compared to Bootstrap

<div class='img-thumbnail'> <img src="a.jpg" class='img-fluid'> </div> This particular code snippet is from an HTML file using Bootstrap 4 CSS. It displays an image inside a styled box. Adding an h4 heading places ...

Issue with displaying random 1px background image on Chrome browser

I'm facing an issue with the background image not appearing for one of the links in my navbar in Chrome. Specifically, the image doesn't show up between the "Athletics" and "Bowling" links. Interestingly, the background image displays fine in Fi ...

Can a media query be activated based on a specific variable value?

I am looking to hide an element on my webpage if the screen width is less than 1000px and a specific variable is above 15. Here is the CSS code I have so far: @media (max-width: 1000px){ #specific-id{ display:none } } However, I need to incorpo ...