Skip to main content

Tooltip Component Using Java Script & CSS

A simple, small Java Script Component, which you can use to have custom Tooltips on your web page. Well why wait, let get right on to it. First lets see the styles used by the Tooltip popup box.
.toolTipDef, .toolTip { 
            position: absolute; 
            background-color: #ccc; 
            border: 1px solid #000; 
            padding: 3px; 
        }
.toolTipDef { display: none; }
.toolTip { display: block; }
All together we’ve to CSS classes, one ‘toolTipDef’ and ‘toolTip’. ‘toolTipDef’ is used when the tooltip need to be hidden, and when user mouse over the text, we’ll use ‘toolTip’ class to make the tooltip popup visible. These styles set standard attributes like background color for the tooltip popup, its border and text padding inside tooltip popup. ‘toolTipDef’ set the ‘display’ property to ‘none’ so that by default the popup is hidden and ‘toolTip’ sets the ‘display’ property to ‘block’ so that when used the tooltip popup gets displayed.
To display tooltip text, we need a container to be used as popup with the given text for this we need to add the below given empty ‘div’ tag to the HTML page.
<div id="toolTip" class="toolTipDef"></div>
If you observe, the default CSS style class used for this ‘div’ is ‘toolTipDef’ which makes this ‘div’ hidden by default. We need to give some value for the ‘id’ attribute of this container div, so that we can access it using the java script and change its content and CSS styles. Here its given as ‘toolTip’, this value will be used while initializing Tooltip Java Script Component.
The last and core part of this component is the Java Script, lets see what we’ve here.
(function() {
    Tooltip = {
        init: function(elId) {
            if (!elId)
                return;
            var el = document.getElementById(elId);
            if(!el)
                return; 
            this.el = el;             this.isIE = document.all? true : false;         }, // End of init()
        showTip: function(anchor, text) {             this.el.innerHTML = text;             this.el.className = "toolTip";
            var pos = this.getElPosition(anchor);
            this.el.style['left'] = pos.x + "px";             this.el.style['top'] = (pos.y + anchor.offsetHeight - 1) + "px";         }, // End of showTip()
        hideTip: function (anchor) {             this.el.className = "toolTipDef";         }, // End of hideTip()
        getElPosition: function(anchor) {             for(var zx=zy=0; anchor!=null; zx+=anchor.offsetLeft,zy+=anchor.offsetTop,anchor=anchor.offsetParent);             return {x:zx,y:zy}         } // End of getElPosition()     } // End of Tooltip })();
This ‘Tooltip’ Java Script class has 4 methods init, ‘init()’, ‘showTip()’, ‘hideTip()’, and ‘getElPoistion()’. The ‘init()’ method initializes the component, it takes the ‘id’ attribute value of the tooltip popup container ‘div’ element. In this example we’ve given it as ‘toolTip’ as mentioned in the above HTML code. This ‘init()’ method checks the given value, if its not provided returns immediately, otherwise it tries to get the DOM reference of this element. If it fails to get the DOM reference, it returns with out giving any error, otherwise keeps this reference in ‘el’ object of this component.
The ‘getElPosition()’ method is used to get the position of the element on the screen for which the tooltip is being displayed, it takes the element as parameter and uses ‘offsetLeft’, ‘offsetTop’, and ‘offsetParent’ attribute values to give the top, left positions of the given element.
The ‘showTip()’ method is used to display the tool tip popup when user, mouse over an element. This method takes the element and the tool tip text as parameters. It sets the ‘innerHTML’ of the popup container ‘div’ with the given text, and sets the CSS class name of the container ‘div’ to ‘toolTip’ which makes it visible. Then it tries to get the position of the given element by calling ‘getElPosition()’ method, and changes the ‘top’ and ‘left’ CSS style attributes with the values returned by the ‘getElPosition()’ method.
The last method ‘hideTip()’, is used to hide the tool tip popup when user, mouse out from an element. This method takes the element for which the tooltip was displayed. This method simply changes the CSS class name of the given element to ‘toolTipDef’ which hides the tool tip container ‘div’.
The last step is to initialize this component on window ‘onload’ event. You can use the sample code given below OR have your own way of doing it.
window.onload = function () { Tooltip.init('toolTip'); };
Only thing you need to make sure is, the parameter to the ‘init()’ method, it should be the ‘id’ attribute value of the popup container ‘div’.
To show tool tip for a HTML element you call the ‘showTip()’ and ‘hideTip()’ method on ‘mouseover’ and ‘mouseout’ events. Sample code on how to do it is given below
<h2 onmouseover="Tooltip.showTip(this, 'This is a Sample Tooltip');"
    onmouseout="Tooltip.hideTip(this);">Move your mouse on to me!!!</h2>
That is all you need to do, Simple isn’t it!

Comments

Popular posts from this blog

Simple Accordion using Java Script and CSS

Well you can find many online, but it's difficult to find one with out any dependent API. Most of the available accordions use other APIs for animation and other stuff. For those who just want accordion with out any other stuff, this one is the perfect one. It's very simple and you don't have to be a geek to understand it. Basic knowledge of Java script and CSS will do to understand how this works. In this article I'll take you through the steps of developing an accordion. Couple of minutes you are ready to write your own. Well then let's start developing one. Layout of the HTML block looks something like the one below Lets look at the CSS first which is very simple. /** container styles **/ .ra-p, .ra-cp { padding: 0 0 0.1em; } /**** heading styles ****/ .ra-p h2, .ra-cp h2 { margin: 0px; padding: 0.2em; cursor: pointer; color: #fff; background-color: #3d80b0; } /**** collapsed heading styles ****/ .ra-cp h

Hosting Multiple Domains In Tomcat

Tomcat allows us to host multiple domains in one instance, using multiple ' Host ' tags. In this article I will explain how to do it on Tomcat. This is very simple configuration using ' Host ' tags in your server.xml . A novice can also understand this configuration very easily. Before going into the details of the configuration first lets have a look at the ' Host ' tag, ' Context ' tag and ' Alias ' tags first. <Host name="domain1" appBase="[application base]" autoDeploy="[true/false]" unpackWARs="[true/false]"> <Alias>...</Alias> <Context path="" docBase="" reloadable="[true/false]"/> </Host> First lets have a look at ' Alias ' tag. This tag is used to provide aliases for your actual domain. For example you have a domain called 'domain1.com', and you want to run the same application for 'www.do