View Full Version : Understanding how does a tooltip using jquery works

09-08-2010, 08:06 PM

I have found on the internet a really cool tooltip (not from dynamicdrive)

Where I found it they dind't give any explanation about how to use it for newbies like me..

First code:

* simpleTooltip jQuery plugin, by Marius ILIE
* visit *** for details
(function($){ $.fn.simpletooltip = function(){
return this.each(function() {
var text = $(this).attr("title");
$(this).attr("title", "");
if(text != undefined) {
var tipX = e.pageX + 12;
var tipY = e.pageY + 12;
$(this).attr("title", "");
$("body").append("<div id='simpleTooltip' style='position: absolute; z-index: 100; display: none;'>" + text + "</div>");
if($.browser.msie) var tipWidth = $("#simpleTooltip").outerWidth(true)
else var tipWidth = $("#simpleTooltip").width()
$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
}, function(){
$(this).attr("title", text);
var tipX = e.pageX + 12;
var tipY = e.pageY + 12;
var tipWidth = $("#simpleTooltip").outerWidth(true);
var tipHeight = $("#simpleTooltip").outerHeight(true);
if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");



Read me:

Usage instructions

- if the element has a TITLE attribute:

I really don't understand :( how does it apply on a text, image etc.?

09-08-2010, 10:15 PM
This is a simple way to set it up:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="simpletooltip.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.tooltip.v.1.1.js"></script>
<script type="text/javascript" src="jquery.tooltip.execute.js"></script>
<a href="#" title="This is the tooltip!" class="with-tooltip">Display tooltip on mouseover</a>

here is a DEMO (http://azoomer.com/jquery.simpletooltip.v.1.1/indexx.html)

09-09-2010, 11:04 AM
Thank you azoomer :D

I don't really understand why shoud I upload the .css file, works fine with <style> tag

09-09-2010, 02:15 PM
Yes, on page styles will also work just as well

09-09-2010, 02:30 PM
The linking of css is better because if you decide to change the style later you will have to edit each page manually. With the linked method you only have to edit that css file. Using the <style> tag in the head is the same, it's about the same as using the style attribute inline though.

09-09-2010, 04:39 PM
Ok, I understand now :)