Click here to Skip to main content
15,887,596 members
Articles / Web Development / HTML
Tip/Trick

Creating Simple JQuery Tooltip

Rate me:
Please Sign up or sign in to vote.
4.81/5 (12 votes)
27 Oct 2014CPOL 15.7K   285   10   1
Creating a very simple and flexible Tooltip with any HTML, CSS, JQuery versions!

Introduction

This tip is about creating a very simple and cute tooltip with any HTML, CSS, JQuery versions!

The download source file above contains a Visual Studio 2013 project! But this JQuery library does not depend on C#, Visual Studio. So you can use this library in any technology or languages such as PHP, JSP, ASP.NET or even HTML.

Using the Code

Note that my JQuery Selector in the below code is a[title]. But you can use any selector that you want. For example img[title], or combination of a[title], img[title], etc.

JavaScript
<script src="Scripts/jquery-2.1.1.min.js"></script>

<script type="text/javascript">

    $(function () {

        var varToolTip = $("<div id='DtxToolTip'>");

        varToolTip.hide();

        $("body").append(varToolTip);

        $("a[title]").each(function () {

            var strTitle = $(this).attr("title");
            $(this).removeAttr("title");
            $(this).attr("data-tooltip", strTitle);

        });

        $("a[data-tooltip]").bind("mouseenter", function (e) {

            // **************************************************
            var varMargin = 2;
            var varBodyWidth = $("body").width();

            var varWidth = $(this).width();
            var varHeight = $(this).height();

            var varTop = $(this).offset().top;
            var varLeft = $(this).offset().left;

            var varWidthOfToolTip = varToolTip.width();
            var varTopOfToolTip = varTop + varHeight + varMargin;

            var varDifference = varWidth - varWidthOfToolTip;
            var varLeftOfToolTip = varLeft + (varDifference / 2);

            if (varLeftOfToolTip < varMargin) {
                varLeftOfToolTip = varMargin;
            }
            else {
                if (varLeftOfToolTip + varWidthOfToolTip > varBodyWidth - varMargin) {
                    varLeftOfToolTip = varBodyWidth - varWidthOfToolTip - varMargin;
                }
            }
            // **************************************************

            var strTitle = $(this).attr("data-tooltip");

            varToolTip.html(strTitle);

            varToolTip.css("top", varTopOfToolTip);
            varToolTip.css("left", varLeftOfToolTip);

            varToolTip.hide();
            varToolTip.fadeIn(400);

        });

        $("a[data-tooltip]").bind("mouseout", function (e) {

            varToolTip.fadeOut(200);

        });

    });

</script>

By the way, you can easily change the style of ToolTip.

CSS
<style type="text/css">

    div#DtxToolTip {

        top: 0px;
        left: 0px;
        z-index: 1000000;
        position: absolute;

        width: 200px;
        min-height: 30px;

        margin: 0px;
        padding: 4px;
        border-width: 2px;
        border-style: outset;
        border-color: #CCCCCC #808080 #808080 #CCCCCC;

        color: Blue;
        background-color: #CCCCFF;

        font-size: 8pt;
        font-family: Verdana;

    }
</style>

History

This is version 1.1: In this version, I fixed some bugs and optimized source code.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Sematec Ins.
Iran (Islamic Republic of) Iran (Islamic Republic of)
My experiences are:

HTML 5.0, CSS 3.0
JQuery, Angular JS, Bootstrap

MVC 5.0, WEB API, c#

My Site URLs:
http://www.IranianExperts.ir
http://www.IranianExperts.com

My Yahoo Group URL: http://groups.yahoo.com/group/iranianexperts

Mobile: 0098-912-108-7461
Address: Tehran, Tehran, Iran

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun27-Oct-14 18:00
Humayun Kabir Mamun27-Oct-14 18:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.