Postado por Fernando Schimit - Fox Creative
![Making a triangular shape with a div Making a triangular shape with a div](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_uJHGXdftrKFx8OZE44aNv0VxETrl4Ldo8M2SbPa8KLl7kTkCTDnMpda1fviOHKnFb4YXN-IbKpg_VaD8Ig4NF87wKOBJI-9PWYaqDGUXoKO8NO1PQ0ZmWU9lgT7g=s0-d)
In this tutorial we are going to write a simple jQuery tooltip plugin. It is going to convert the title attributes of elements withing your page, into a series of colorful tooltips. Six color themes are available, so you can easily match it with the rest of your design.
Step 1 – XHTML
The plugin we are about to write today, works by converting the title of an element on the page to a structure of three spans, which form a tooltip, displayed on hover. So if you start with a regular link like this one:
jQuery will convert it to the markup you can see below.
2 | < span class = "colorTip" style = "margin-left: -60px;" >Go to Tutorialzine |
3 | < span class = "pointyTipShadow" >span > |
4 | < span class = "pointyTip" >span > |
Notice that the first block of code above specifies a “
blue” class name. This is because we are overriding the default color of the tip (yellow). You can choose from
red,
blue,
green,
yellow,
white and
black, or you can create your own colors in the plugin stylesheet.
Users which have JavaScript disabled are still going to see the regular title tooltips on the page, so the script degrades gracefully.
Step 2 – CSS
To display the colorful tips on your page, you first need to include the plugin stylesheet file in the head section of the html document.
1 | < link rel = "stylesheet" type = "text/css" href = "colortip-1.0/colortip-1.0-jquery.css" /> |
You could also just copy the styles from the file and paste them directly in your main stylesheet, if you do not want to keep a separate include file. This stylesheet defines both the positioning and the design of the tooltips. It comes with six color themes, and you can easily add more.
colortip-1.0-jquery.css – Part 1
03 | text-decoration : none !important ; |
15 | background-color : white ; |
16 | font-family : Arial , Helvetica , sans-serif ; |
22 | text-shadow : 0 0 1px white ; |
25 | -moz-border-radius: 4px ; |
26 | -webkit-border-radius: 4px ; |
30 | .pointyTip,.pointyTipShadow{ |
32 | border : 6px solid transparent ; |
The
.colorTipContainer class is assigned to the elements to which the color tips are added. It sets their positioning to relative, so that the tips can be
centered above them.
A neat CSS trick is used to create the triangular pointy tips. As you know, divs and spans can only take a rectangular shape (or an ellipse / rounded rectangle if you apply border-radius). However, if you apply a thick border to a 0 by 0 element, the only way the borders are going to fit is to take up a triangular space on each side. Then, by setting the default border color to transparent, you can make visible any of the four triangular shapes.
Making a triangular shape with a div
This is actually done on both the .
pointyTip and the .
pointyTipShadow spans, in order to give an impression that the pointy tip has a border applied so it matches the rest of the colortip design. Now lets take a closer look at the six color themes.
colortip-1.0-jquery.css – Part 2
03 | . white .pointyTip{ border-top-color : white ;} |
04 | . white .pointyTipShadow{ border-top-color : #ddd ;} |
06 | background-color : white ; |
07 | border : 1px solid #DDDDDD ; |
11 | .yellow .pointyTip{ border-top-color : #f9f2ba ;} |
12 | .yellow .pointyTipShadow{ border-top-color : #e9d315 ;} |
14 | background-color : #f9f2ba ; |
15 | border : 1px solid #e9d315 ; |
19 | . blue .pointyTip{ border-top-color : #d9f1fb ;} |
20 | . blue .pointyTipShadow{ border-top-color : #7fcdee ;} |
22 | background-color : #d9f1fb ; |
23 | border : 1px solid #7fcdee ; |
27 | . green .pointyTip{ border-top-color : #f2fdf1 ;} |
28 | . green .pointyTipShadow{ border-top-color : #b6e184 ;} |
30 | background-color : #f2fdf1 ; |
31 | border : 1px solid #b6e184 ; |
35 | . red .pointyTip{ border-top-color : #bb3b1d ;} |
36 | . red .pointyTipShadow{ border-top-color : #8f2a0f ;} |
38 | background-color : #bb3b1d ; |
39 | border : 1px solid #8f2a0f ; |
44 | . black .pointyTip{ border-top-color : #333 ;} |
45 | . black .pointyTipShadow{ border-top-color : #111 ;} |
47 | background-color : #333 ; |
48 | border : 1px solid #111 ; |
Remember that only the borders are the only visible part of the pointy tips. You can add your own color themes by following the same structure.
Step 3 – jQuery
First you need to include the jquery library and the plugin files to the page, after which our script.js file, which is going to utilize the plugin and add colortips to the links on the page.
2 | < script type = "text/javascript" src = "colortip-1.0/colortip-1.0-jquery.js" >script > |
3 | < script type = "text/javascript" src = "script.js" >script > |
For performance reasons, I’ve put this code at the bottom of the
colortips.html file. This allow for the page design to be rendered before the browser blocks for the loading of the js files.
Now lets take a look at the colortip the plugin.
colortip-1.0-jquery.js – Part 1
02 | $.fn.colorTip = function (settings){ |
04 | var defaultSettings = { |
09 | var supportedColors = [ 'red' , 'green' , 'blue' , 'white' , 'yellow' , 'black' ]; |
12 | settings = $.extend(defaultSettings,settings); |
19 | return this .each( function (){ |
24 | if (!elem.attr( 'title' )) return true ; |
29 | var scheduleEvent = new eventScheduler(); |
30 | var tip = new Tip(elem.attr( 'title' )); |
35 | elem.append(tip.generate()).addClass( 'colorTipContainer' ); |
43 | if (elem.hasClass(supportedColors[i])){ |
52 | elem.addClass(settings.color); |
When you call the plugin, you can pass a configuration object, which holds the default color and the timeout after which the tooltips disappear on mouseleave.
You can choose from six possible colors and assign them as a class name to the element. The plugin would check if a color class is present, and if it is not, it will apply the default one you’ve passed in the config object. If you’ve not passed a config object, yellow will be used instead.
Colortip jQuery Plugin
colortip-1.0-jquery.js – Part 2
04 | elem.hover( function (){ |
11 | scheduleEvent.clear(); |
18 | scheduleEvent.set( function (){ |
27 | elem.removeAttr( 'title' ); |
36 | function eventScheduler(){} |
38 | eventScheduler.prototype = { |
39 | set : function (func,timeout){ |
44 | this .timer = setTimeout(func,timeout); |
50 | clearTimeout( this .timer); |
In the second part of the plugin code, we bind event listeners to the hover event (combination of a mouseenter and a mouseleave event).
Notice the event scheduler class. It is used to set a function to be executed after a predetermined period of time. At its heart lays a call to
setTimeout. A
clear() method is also provided as a part of the class, so previously scheduled events can be destroyed (useful when you move your mouse away from a tooltip and then move it back over before it has disappeared).
colortip-1.0-jquery.js – Part 3
17 | return this .tip || ( this .tip = $( '' + this .content+ |
21 | if ( this .shown) return ; |
24 | this .tip.css( 'margin-left' ,- this .tip.outerWidth()/2).fadeIn( 'fast' ); |
In the last part of the code we define the tip class. It has a
generate,
show and
hide methods. An object from this class is created for each of the tooltips. The generate method is called first, and the tooltip is stored in the local
this.tip variable. The show and hide methods operate on this variable to run fade in/out animations.
Now we are only left with calling the plugin and adding colortips to all the links on the page.
script.js
1 | $(document).ready( function (){ |
5 | $( '[title]' ).colorTip({color: 'yellow' }); |
The configuration object here might as well be omitted, because yellow is the default value anyway. But you could specify a different color for the tooltips. If you apply
red, green, blue,
white,
yellow or
black as a class name of the element, the design of the tooltip will be overridden.
With this our Colortip plugin is complete