Show Tooltip On Mouse Position in JavaScript

Show Tooltip On Mouse Position in JavaScript
Code Snippet:
Author: sdafasdfasdf
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 727
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to show a tooltip on the mouse position on a webpage. It uses clientX and clientY properties of the mouse event to get the x and y coordinates of the cursor then sets the top and left CSS properties of the tooltip element.

The tooltip comes with a simple box that appears when the user hovers over an element. You can integrate this snippet to display additional information about the element, such as a description or a link. Moreover, you can customize the tooltip element with additional CSS according to your needs.

How to Show Tooltip On Mouse Position in JavaScript

1. First, create a container for your image and tooltip. Use the following HTML structure as a starting point:

<div class="container">
    <div class="img-block shadow-effect">
        <img class="image" src="https://www.telegraph.co.uk/content/dam/food-and-drink/2017/01/04/coffeeS006G8_wwwAlamycom_Heart-shaped-coffee-art-on-a-latte_trans_NvBQzQNjv4BqEDjTm7JpzhSGR1_8ApEWQA1vLvhkMtVb21dMmpQBfEs.jpg">
        <span id="tooltip">❤ Coffee</span>
    </div>
</div>

2. Apply the necessary CSS styles to ensure the proper layout and appearance. You can customize the styles according to your website’s design:

@import url(https://fonts.googleapis.com/css?family=Open+Sans);

.container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 95vh;
    width: 100%;
}

.img-block {
    max-height: 80%;
    width: auto;
    cursor: pointer;
    display: flex;
    justify-content: center;
    position: relative;
}

.image {
    max-height: 100%;
    width: auto;
    max-width: 100%
}

#tooltip {
    position: fixed;
    height: fit-content;
    width: fit-content;
    background-color: white;
    padding: 5px 12px;
    box-shadow: 0 0 2px rgba(0,0,0,0.2);
    border-radius: 5px;
    font-family: 'Open Sans';
    display: none;
}

.img-block:hover #tooltip {
    display: block;
}

.shadow-effect {
    position:relative;
    -webkit-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
       -moz-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
            box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;
}
.shadow-effect:before, .shadow-effect:after {
    content:"";
    position:absolute;
    z-index:-1;
    -webkit-box-shadow:0 0 20px rgba(0,0,0,0.8);
    -moz-box-shadow:0 0 20px rgba(0,0,0,0.8);
    box-shadow:0 0 20px rgba(0,0,0,0.8);
    top:5px;
    bottom:0;
    left:10px;
    right:10px;
    -moz-border-radius:100px / 10px;
    border-radius:100px / 10px;
}
.shadow-effect:after {
    right:10px;
    left:auto;
    -webkit-transform:skew(8deg) rotate(3deg);
       -moz-transform:skew(8deg) rotate(3deg);
        -ms-transform:skew(8deg) rotate(3deg);
         -o-transform:skew(8deg) rotate(3deg);
            transform:skew(8deg) rotate(3deg);
}

3. Finally, integrate the following JavaScript code to enable the dynamic positioning of the tooltip based on the mouse cursor:

let toolTip = document.getElementById('tooltip');

window.addEventListener('mousemove', toolTipXY);

function toolTipXY(e) {
    let x = e.clientX,
        y = e.clientY;
    toolTip.style.top = (y + 20) + 'px';
    toolTip.style.left = (x + 20) + 'px';
};

That’s all! hopefully, you have successfully created a functionality to display the tooltip on the cursor position using JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X