Tips for creating a floating Tooltip that follows the Cursor on a wider element

In my application, I have a Gantt chart with horizontal scrolling capabilities. Users can navigate through the chart by using the scrollbar, drag and drop feature, or mouse wheel.

The scrollable area of the Gantt chart can be very wide, spanning thousands of pixels in width.

One issue I encountered is related to an HTML element that extends beyond the visible screen width. When trying to use a tooltip library on this element, the tooltips are centered which requires users to scroll towards the middle of the screen to view them. This often results in the tooltip being out of view.

I tried implementing various tooltip libraries without success. Currently, I am experimenting with lightweight jQuery solutions in the hope of finding a suitable tooltip solution that floats around the cursor position.

The image below illustrates the problem of tooltips appearing out of view on my Gantt chart:

View full size image


Initial Testing....

A simple demo showcases a basic floating tooltip following the mouse cursor's movement.

I have included another demo at the bottom of this question highlighting the issue version.

http://jsfiddle.net/jasondavis/L2gmcxhc/

HTML structure for floating tooltips:

<div class="item">
    <p>This is my item</p>
    <div class="tooltip">Tooltip</div>
</div>

jQuery code for tooltip functionality:

$(document).ready(function() {

    $(".item").mousemove(function(e) { 

        $('.tooltip').css('left', e.pageX + 10).css('top', e.pageY + 10).css('display', 'block');

    });

    $(".item").mouseout(function() { 
        $('.tooltip').css('display', 'none');
    });

});

CSS styling for tooltips:

.item {
    position: relative;
    width: 100px;
    height: 40px;
    top: 200px;
    left: 400px;
    background: #CCC;
}

.item .tooltip {
    position: fixed;
    width: 80px;
    height: 30px;
    background: #06F;
    z-index: 10;
    display: none;
}

Update:

Upon testing the second demo http://jsfiddle.net/jasondavis/L2gmcxhc/1/, it became evident that when the element requiring the tooltip hovered upon was wider, such as in a Gantt chart scenario, the tooltip failed to float accurately along with the cursor. As a result, the tooltip would get lost when scrolling horizontally.

I am seeking an easy fix or solution to ensure the tooltips remain aligned with the cursor even when the page width extends beyond the usual viewport. Any assistance or suggestions would be greatly appreciated!

Answer №1

Consider utilizing the following code snippet:

$('.tooltip')
    .css('left', e.pageX - $(window).scrollLeft() + 10)
    .css('top', e.pageY - $(window).scrollTop() + 10)
    .css('display', 'block');

Answer №2

If you have some spare time ...

Try incorporating native browser tooltips into your design. Simply add a title attribute to the desired element for a tooltip.

Check out this demo

Crafting Custom Styles for Tooltips

You have the flexibility of using CSS to customize the appearance of your tooltips: Helpful tips here

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

Position of Bootstrap glyphicon

The issue at hand I am trying to find a way to display a glyphicon icon after a text. The documentation only provides examples for displaying icons before the text, as shown here: http://getbootstrap.com/components/#glyphicons-examples <div class="gly ...

Unable to access the POST value in the current context

I am having trouble retrieving the values from a simple form that consists of two files: APP.js and FORM.ejs. I am attempting to get the values submitted through the form. APP.js: const http = require('http'); const express = require("express"); ...

Styling group headers within an unordered list using pure CSS - possible?

Hey there, I'm looking to spruce up my UL by adding group headers. Here's a sneak peek at the structure I have in mind (keep in mind this is generated dynamically from a database): <ul> <li class='group group-1'> < ...

Prevent mobile view from activating when zoomed in on the screen

I've built a webpage with a responsive design that adjusts to mobile view on mobile devices or when the screen size is reduced using developer tools. While this functionality works correctly, I have noticed that the design also switches to mobile vie ...

Displaying a MySQL blob image in an HTML file with Vue.js: A step-by-step guide

Here is a Vue file that I have: export default { data(){ return{ info: { name: '', image: '', }, errors: [] } }, created: function(){ thi ...

What is the way to access the "bio" property of a "User" object using JavaScript?

How can I retrieve the "bio" value from the "User" object? Refer to the image below for clarification: View the object here After running this code using the discordjs-selfbot-v13 npm package, I received the following object: See the code here. I attemp ...

The CSS background image in PNG format displays a white line at the beginning of the transparency

How can I remove the white border that appears on PNG images with transparency when used as a background with CSS on retina devices? .background { background: url('../image.png') no-repeat; } ...

When downloading files that I uploaded to Google Drive using React Native, I noticed that the file size is not consistent with the original file

Greetings Everyone! I am aiming to transfer the database (RealmJS) that is currently stored on my device storage to Google Drive using Google Drive API V3. To accomplish this, I have already installed various node modules such as react-native-fs and @reac ...

Using the getElementById function in javascript to modify CSS properties

Here is the setup I am working with: <div id="admin"> This element has the following style applied to it: display: none; I am attempting to change the display property when a button is pressed by defining and utilizing the following JavaScript co ...

Tips on integrating JavaScript in Laravel using Vue.js 3

Seeking advice on the best way to include JavaScript files in a Laravel and VueJS project. I've tried one method from a YouTube video, but it's not always reliable and might not be the best practice. Any alternative suggestions? ...

Modify content on a link using AngularJS

Currently, my setup looks like this: HTML Code <div ng-app="home" ng-controller="customersCtrl"> <div ng-bind-html="home" id="content">{{content}}</div> </div> JS Code angular.module('home', []).controller('c ...

Background image changing due to React re-render

I'm working on a React component that generates a random background image each time a user visits the page. However, I'm facing an issue where the background image updates every time a user types something into an input field. I've tried usi ...

Ways to remedy the white line produced by CSS transform when hovered over?

Has anyone discovered a fix for the white line or border that appears when an element is transformed with CSS scale on hover? I've tried various solutions such as: transform: translateZ(0), -webkit-backface-visibility: hidden, transform-style: preser ...

Checking the validity of date inputs - microservice for timestamps

I'm encountering an issue while attempting to validate my date input. I've tried using moment js, but it seems there's a problem. The error message "date invalid" keeps popping up! Here is the code snippet: app.get("/api/timestamp/:date_s ...

Changing the class of an element in Svelte: A step-by-step guide

I am working on a svelte application and I want to implement row highlighting when a user clicks on it. Here is an example code snippet that demonstrates this functionality: <div id="A" class="car-even">A</div> <div id=&q ...

Preventing select from opening when closing chip in Vuetify: A simple guide

Looking at this specific situation on vuetify.com https://codepen.io/anon/pen/RqoxXY?&editors=101 Regarding the autocomplete feature with chips, is there a way to prevent the select menu from opening when I cancel a chip? I attempted using @click.st ...

Looking for a Jquery method to create a smooth hide/show transition for expanding elements?

Hello, I'm looking for a jQuery solution that can achieve a similar sidebar effect like the one on this website. It seems to be some kind of easing expandable effect. Any suggestions? Thank you! - Judi ...

What is the reason behind the issue where max-height disrupts border-radius?

My inline svg is styled with the following CSS: .img { border-radius: 15px; overflow: hidden; max-width: 70vw; margin-top: 6vh; max-height: 50vh; z-index: -1; text-align: center; } Everything seems to work as expected, except f ...

When choosing an option, the First Table select instantly leaps across the screen

I am currently working on a project where I need to dynamically display certain rows in a table based on the report chosen. However, I'm facing an issue where the first select element is getting pushed to the right side of the screen when a report is ...

Creating a dialog box in jQuery triggered by a user clicking a link

Just starting out with JQuery and curious about creating a dialog box that appears when a user clicks on a link. For example, if I click "send," I want a dialog box to pop up saying "your information has been sent!" Any assistance is greatly appreciated! ...