I love jQuery and the way it makes developer's life easier. Although it took me a while to accept it and I still prefer to write my own stuff, I can't deny its advantages. Recently I had a project that demanded a rollover image preview. You know, one of those tooltip-like bubble popups that appears when you roll over link or a thumbnail. Since we were already using jQuery on that project I decided to take it easy and investigate what can be done with that extraordinary library. So I came up with a script so simple it hurts! The best thing yet is that it can be applied for a variety of purposes. Today I will show you 3 examples of using the same very, very simple script.
Download all 3 tricks in a bundle
Features and Principle
What this script does is adds an element to the body when you roll over a certain object. That element's appearance is predefined with css (positioned absolute and styled) so all we need to do is fill the element with content, calculate mouse position and make it move accordingly. When cursor is moved over he object, element moves with it and when cursor roll out, the element is deleted.
Here are couple of examples where you can see this script in action.
Example 1: The Simplest jQuery Tooltip Ever
The script takes a title attribute of an A tag and place it inside the popup element.
Html looks like this:
<a href="http://cssglobe.com" class="tooltip" title="Web Standards Magazine">Roll over for tooltip</a>
Example 2: Image Preview Gallery
Here we have a bunch of thumbnails. When each thumbnail is rolled over, script will load an image that we have linked in HREF attribute of an A tag. That makes this method accessible as well, because user can still access the target image even with disabled JavaScript.
Html:
<a href="1.jpg" class="preview"><img src="1s.jpg" alt="gallery thumbnail" /></a>
or if you want to use caption add a title attribute:
<a href="image.jpg" class="preview" title="Great looking landscape">Roll over to preview</a>
Example 3: Links With URL Preview
This demands a bit more effort but it might be worth it as an extra feature to add to your sites. What you'll need here is a small size screenshot of the target url. You'll put screenshot image location in in REL attribute of the A tag and script will do the rest.
Like so:
<a href="http://www.cssglobe.com" class="screenshot" rel="cssg_screenshot.jpg">Css Globe</a>
again, if you wish to use caption add a title attribute:
<a href="http://www.cssglobe.com" class="screenshot" rel="cssg_screenshot.jpg" title="Web Standards Magazine"> Css Globe</a>
David Appleyard 8 May, 2008
Steph 8 May, 2008
Boris 8 May, 2008
YellowLlama 9 May, 2008
Janko 9 May, 2008
Ivan 9 May, 2008
Dragon 10 May, 2008
Josh Darville 10 May, 2008
www.TheMediaZoo.com
my name is Josh
JoshD@themediazoo.com
Subash 11 May, 2008
Thanks Alen.
webpixelkonsum 11 May, 2008
Thank you for your nice work.
Ralph
AndreiZ 11 May, 2008
Senko 12 May, 2008
http://blog.senko.net/static/jquery.csstooltip.js
Osvaldo 13 May, 2008
Juan 14 May, 2008
Sorry as I'm a newbie with Javascript: How could I make the big image displaying on top or to the left instead of to the right of the small image?
paul 16 May, 2008
snetcher 17 May, 2008
ana gomez 20 May, 2008
payastr 22 May, 2008
Andrew Cornett 22 May, 2008
Vlad 26 May, 2008
There is one problem with this script - for some reason if the script is outside of an UpdatePanel (Ajax.net) it sometimes "gets confused" and removes the title value but never returns it or shows title value of one link on other one (because this.t is never gets erased for some reason)
did anyone encounter these issues?
does anyone have a fix?
Thanks again
evodesign 28 May, 2008
Thanks
limitless86 29 May, 2008
Masey 29 May, 2008
Any help here would be greatly appreciated.
intelligenc 1 Jun, 2008
Charles 3 Jun, 2008
Is there a way to simply show the content of a hidden div in this tooltip ?
Thank you
Charles
Cameron 9 Jun, 2008
Dalain 10 Jun, 2008
I used the id tag as my href to allow a preview and also the ability to link to another page.
Useful if you have a thumbnail of an image, and you want to link to an actual page.
Still working on an image map solution.
Jonathan Concepcion 14 Jun, 2008
Sun 14 Jun, 2008
Nice code!I like it very much.
Sorry for my poor English .
Alex 19 Jun, 2008
Thank you, Thank you, Thank you!!
Dave 20 Jun, 2008
Muneef Hameed 20 Jun, 2008
i like it!
Olivia Bell 21 Jun, 2008
Ravi 21 Jun, 2008
Ravi.
Abayomi Oyewumi 23 Jun, 2008
u just added to a life
greeeeeeeeeee
Albano 24 Jun, 2008
Fred Riley 27 Jun, 2008
<a href="index.php" title="This is the home page">Home</a>
Both IE7 and Firefox 2 render this as a tooltip on mouseover. Ok, it's not strictly what the title attribute should be used for, but it's fairly common practice these days. Think of it as 'off-label' tagging ;-)
Fred
Umer Tahir 4 Jul, 2008
http://jim-murray.ca/shows.html
Just hover over some of the underlined shows.
Enrique Espinosa 8 Jul, 2008
LJ 8 Jul, 2008
cyberbuff 10 Jul, 2008
James Breen 10 Jul, 2008
http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fcssglobe.com%2Flab%2Ftooltip%2F01%2F
Thanks
Jim
Mark Abucayon 13 Jul, 2008
gnysek 13 Jul, 2008
I don't belive that it's so easy!
Alex 15 Jul, 2008
$("#preview")
.fadeIn("fast",function(){
$("#preview")
.css("left",(e.pageX + xOffset) + "px")
.css("top",(e.pageY - $("#preview").height()) + "px");
yOffset = $("#preview").height();
});
Maybe there Is a better way?
People Search Dude 17 Jul, 2008
Clean implementation too!
Toxane 20 Jul, 2008
Em 21 Jul, 2008
Unfortunately, there are no instructions, so I'm not quite sure how to connect the js files in the download with the textarea cut and paste code.
steve 23 Jul, 2008
steve 23 Jul, 2008
Ranjit 24 Jul, 2008
mark 27 Jul, 2008
Has anyone figured this out? I don't want to have to resize my rollover images but so far I can't think of any other way to keep the images inside the window.
HELP!!!
Firebubble Design 27 Jul, 2008
Sander Houttekier 28 Jul, 2008
because your images are perfectly the same width you dont have a problem, but as soon as there are images with other proportions it gives troubles... i did already make sure that no image went over the given width... but smaller images now have lots of whitespace around them...
is it possible to make the width of the preview div, automaticaly use the width of the image itself?
josef 30 Jul, 2008
can anyone help me how to fix it? i'd like the image to open upwards - thanks!
Website Designer Perth 31 Jul, 2008
raminhos 6 Aug, 2008
Many tks for this must :)
Keep the good work
moshe 6 Aug, 2008
great script
has anyone managed to solve the issue that mark mentioned above - that the tooltip sometimes appears off screen>?
thanks!
josh 8 Aug, 2008
Fiyat 8 Aug, 2008
sjl 11 Aug, 2008
Thanks for the post.
rob 12 Aug, 2008
All the best and thanks for sharing your knowledge.
disenando 12 Aug, 2008
I had the same problem but I do know the minimum pagesize and the large image size and its position to the right, in my case 400px for the image and 758px for the page. What I did is set yOffset = -430; i.e. 30 pixels lower than the image because Firefox has a bug if the image is displayed "below" the pointer cursor (the hand). This works.
If we are to find the solution to a dynamic setting so it automatically detects whether the image will be out to the right, we have to set a condition to show (e.pageX - yOffset > $(document).width()) = align to the cursor's left.
By the way, yOffset and xOffset should be reverted.
Dave 12 Aug, 2008
Hasan Tayyar BEŞIK 14 Aug, 2008
But you must add this line before $("body").append("<p id='tooltip'>"+ this.t +"</p>"); :
if(this.t!="")
egypt 18 Aug, 2008
coderbari 19 Aug, 2008
Poppy 21 Aug, 2008
Eddie 22 Aug, 2008
PS. This would be a realy good WordPress Plugin :)
BLACK SHARK MEDIA 24 Aug, 2008
We've fnd a solution to that:
At x axis put the value 400
At the y axis put the value - 400
So the image will be aligned starting from the top left corner not bottom right corner.
Cheers.
Günther 26 Aug, 2008
web design los angeles 4 Sep, 2008
Anthony 16 Sep, 2008
James Wigger 17 Sep, 2008
Thanks again,
James
mastr3s 19 Sep, 2008
please i really need it i already try many time using this kinda script and put it on php code but work not really nice the image preview wont go away so if anyone one please response to mastr3s@gmail.com
Andrew 25 Sep, 2008
ERROR: Array length must be assigned a finite positive number
gnaffetto 12 Oct, 2008
i've tried immediatly and this is my example (i'm from italy): http://censuralegale.helloweb.eu/docu/
i try: are you interesting to write a mediawiki extension of this script? wikipedia with this feature will be GREAT!! (i've seen an extension that take data from a database:http://www.mediawiki.org/wiki/Extension:WoWItem)
thanks.
gnaffetto
Alexandre 17 Oct, 2008
this is really the easiest tooltip example.
Alexandre
Mark 20 Oct, 2008
cheers
Bruno 20 Oct, 2008
To position the tooltip depending where you are, you need to rewrite some of the code using the offset() property of jQUERY.
var toolTipPosition = $(this).offset(); //Declare the Offset object
var offsetX = 0;
var offsetY = 0;
//Then in the hover property
$("#tooltip")
.css("top",( toolTipPosition.top - posiY) + "px")//Will set where the link/thumbnail is horizontally
.css("left",( toolTipPosition.left + this.offsetWidth/2 + posiX) + "px") /*Will be positioned to the middle of the link/thumbnail, you can alway remove this.offsetWidth/2 to remove the middle placement thing.*/
//Remove the mouseover function and your set!
.fadeIn("fast");
weblover 2 Dec, 2008
Mahmoud M. Abdel-Fattah 2 Dec, 2008
how can I fix this ??
Here's a demo of the problem :
http://alia.hdeya.com/projects/
scott 9 Dec, 2008
Say theres a broken link in main.js " + this.rel + "
any ideas
Webagentur 2 Jan, 2009
Gustavo Baldera 9 Jan, 2009
A.P 13 Jan, 2009
vuelos 14 Jan, 2009
Emanuelle 14 Jan, 2009
Tau 21 Jan, 2009
xaer8 22 Jan, 2009
Hiral Thaker 23 Jan, 2009
I have the same problem. But don't know how to solve this.
What about this z-index. Please let me know. I am not good to design
thanks
Hiral
Chris Barnett 23 Jan, 2009
Thank you so much for this tutorial and effort you put into the script. I am going to try and implement the tooltip now. Hoping it is as easy as you say it is. Wonderful work and always enjoy your posts. Keep up the great work..
Cheers!
Tibor 26 Jan, 2009
T 27 Jan, 2009
Just what I was looking for!
Thanks :)
fesh 27 Jan, 2009
Manny Calavera 3 Feb, 2009
this.tooltip = function(){
/* CONFIG */
xOffset = 10;
yOffset = 20;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */
$(".tooltip").hover(function(e){
this.t = this.title;
this.title = "";
$(this).append("<p id='tooltip'>"+ this.t +"</p>");
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
},
function(){
this.title = this.t;
$("#tooltip").remove();
});
$(".tooltip").mousemove(function(e){
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
};
Hiral Thaker 5 Feb, 2009
I am using this utility and working fine. But one problem..
When I move the cursor on the image some images which are at last displaying the tooltip image after the actual screen. So I can see only half image. I tries to change xOffset and yOffset but still not solved
Please help
Thanks in advance
Milind Ghatkamble 7 Feb, 2009
Xboy 11 Feb, 2009
Edward 15 Feb, 2009
modoko 22 Feb, 2009
karacabey 22 Feb, 2009
Fedeweb 25 Feb, 2009
one question: how can i use a tooltip with different offset in the same page?
Artevideo 26 Feb, 2009
Thanks
Tobias 26 Feb, 2009
Your script is great, I just included it and it works perfectly. I'm using Script #2, the Tooltip for pictures.
But as I'm not too much of an expert, please allow this question:
Is there any way to link to a path instead to the picuture? I mean some kind of workaround maybe?
I would like to use this script for a shop-like application, but of course the clients are supposed to see the detail-page when they click on the picture, instead of a bigger version of this picture.
Thanks in advance for clarifying, and of course thanks for the great script.
All the best from germany!
Tobias
Jim 28 Feb, 2009
Ischia 2 Mar, 2009
Alistair 3 Mar, 2009
peter 5 Mar, 2009
Thank you so much! This was driving me crazy for the last two weeks!
MegaFill 9 Mar, 2009
Anton 11 Mar, 2009
Rafael N Samapio 11 Mar, 2009
Sfondi 16 Mar, 2009
Chanie 16 Mar, 2009
John 18 Mar, 2009
the text display under the next pic or flash object
any idea how to solve??
thanks
zahid 24 Mar, 2009
lamuy 31 Mar, 2009
web design norwich 31 Mar, 2009
GadElKareem 1 Apr, 2009
GadElKareem 1 Apr, 2009
tooltip = function(){
/* CONFIG */
xOffset = 15;
yOffset = 10;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */
$("a[title]").live('mouseover', function(e){
this.t = this.title;
this.title = "";
$("body").append("<p id='tooltip'>"+ this.t +"</p>");
$("#tooltip")
.css("top",(e.pageY + xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
});
$("a[t]").live('mouseout',function(){
this.title = this.t;
$("#tooltip").remove();
});
$("a[t]").live('mousemove', function(e){
$("#tooltip")
.css({ top: (e.pageY + xOffset) + "px", left: (e.pageX + yOffset) + "px"} );
});
};
// starting the script on page load
$(function(){
tooltip();
});
still need a way to move the tooltip up when it reaches the page adges
Steven 2 Apr, 2009
joe 7 Apr, 2009
http://www.kriesi.at/archives/create-simple-tooltips-with-css-and-jquery-part-2-smart-tooltips
gino 8 Apr, 2009
Mahboob ISLAM 16 Apr, 2009
peter 17 Apr, 2009
Ryan 19 Apr, 2009
Emil 19 Apr, 2009
ArthasMX 23 Apr, 2009
Luca Barbonaglia 24 Apr, 2009
I've dowload Links With URL Preview it's beautiful but I've a little problem, the image is on the right of mouse and I would like on the left. This is possible ? If yes tell me about please.
Thank you so much
Luca from Principaute of Monaco
robert 28 Apr, 2009
Anand 30 Apr, 2009
Thank You
Aashish Singh 1 May, 2009
R Valdez 1 May, 2009
zhuge 4 May, 2009
K Nambissan 5 May, 2009
Jason Grant 6 May, 2009
LP 8 May, 2009
Ale 8 May, 2009
thank u
me , myself and i 12 May, 2009
tool tip shows on alt="this.t"
$('#tooltip').html($(this).attr('alt')).show();
i tryed this but i cant get it work
Derek 12 May, 2009
Darthslade 14 May, 2009
P.S. 16 May, 2009
This function looks awesome, but I am completely green at this jQuery or .js thing, so I have no idea how to add this to my website.
I tied this:
I uploaded all the files from 03 folder to my theme files main folder, then in my theme file between <head> tags I added this:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="main.js"></script>
Then in my site I posted link witch looks like this:
<a href="http://www.cssglobe.com" class="screenshot" rel="full link to img">Css Globe</a>
But it is no good... Then I rollover it img does not appear...
What could be the cause of this malfunction?
May be .xml file need so editing?
Please help me I would be very grateful :)
Dale Larsen 19 May, 2009
Which license is it filed under?
TheRose 21 May, 2009
Very fluid and easy to use.
Thanks again for sharing.
XiShaN 22 May, 2009
keep going...
Tim 23 May, 2009
Tim 24 May, 2009
Example #2: I want to adjust the x and y offset.
Example #1: I want to keep the default offsets.
For some reason its reading the offsets in the last js file listed on the page and applying to both. It would seem like you can have a different offset for each kind of tip. Is this a bug?
<script src="js/ss-pview.js" type="text/javascript"></script>
<script src="js/tooltip.js" type="text/javascript"></script>
PiYU 27 May, 2009
This is simplest and easiest method to do it!
Sam 27 May, 2009
mousemove(function(kmouse){
var border_top = $(window).scrollTop();
var border_right = $(window).width();
var left_pos;
var top_pos;
var offset = 15;
if(border_right - (offset *2) >= my_tooltip.width() + kmouse.pageX){
left_pos = kmouse.pageX+offset;
} else{
left_pos = border_right-my_tooltip.width()-offset;
}
if(border_top + (offset *2)>= kmouse.pageY - my_tooltip.height()){
top_pos = border_top +offset;
} else{
top_pos = kmouse.pageY-my_tooltip.height()-offset;
}
my_tooltip.css({left:left_pos, top:top_pos});
Sam 27 May, 2009
x_Offset =10;
y_Offset =20;
And change all instances of xOffset/yOffset to x_Offset/y_Offset too
Dain 28 May, 2009
Anyway, here's my code:
//tooltip refactor with live so i don't have to reinit and break stuff
$("a.tooltip").live('mouseover', function(e){
/* CONFIG */
var xOffset = 40;
var yOffset = 0;
/* END CONFIG */
this.t = this.title;
this.title = "";
$("body").append("<p id='tooltip'>"+ this.t +" <img src='_images/tooltiparrow.png' /></p>");
var tipWidth = ($("#tooltip").width() / 2) + 10 ;
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.css("margin-left", "-"+ tipWidth +"px")
.fadeIn(100, function() {
});
$(this).mousemove(function(e){
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
});
});
$("a.tooltip").live('mouseout', function(e) {
this.title = this.t;
$(this).die("mousemove", "mouseout");
$("#tooltip").remove();
});
Dain 28 May, 2009
Gabriel Verdejo 28 May, 2009
asad 29 May, 2009
Thomas Svensson 29 May, 2009
SLA80 1 Jun, 2009
1. Fixed a BUG. Please, fix it too: Place "if (this.t)" just before "this.title = this.t;". It is possible to reproduce the bug at least in Chrome browser: press "Reload", and then move your mouse pointer FAST to the place, where element with tooltip will appear. The next moment after page reload, standard (non-decorated) tooltip will appear. Move mouse away (TOOLTIP TEXT IS DESTROYED RIGHT NOW), and back to the element. Deporated tooltip appears, but its text is "undefined", because its text was destroyed forever.
2. Replaced $("a.tooltip") with $(".tooltip") , so not only links, but any other element with tooltip (<img>, <span>, etc.) will display decorated tooltips.
Marko Vierimaa 3 Jun, 2009
$(".imageTooltip").mousemove(function(e){
var posY;
if (e.pageY - $(window).scrollTop() + $('#tooltip').height() >= $(window).height() ) {
posY = $(window).height() + $(window).scrollTop() - $('#tooltip').height() - 15 ;
} else {
posY = e.pageY - 15;
}
$("#tooltip")
.css("top",(posY) + "px")
.css("left",(e.pageX + 15) + "px");
});
Eddie 3 Jun, 2009
great script thanks.
I am trying to scale the image that is previewed, however the first time the mouseover event occurs on the image link my pop-up is empty, then the next time it's fine. I know it is the way I am populating the width value of the image that is causing it, but I dont understand why, I thought it processed the code from top to bottom, so it should always get the image size first. All my images are different dimensions that need to be scaled correctly. I decrement by 3% each time until dimensions are under max. Any help (from anyone reading this) would be greatly appreciated.
here's my code:
$("a.preview").hover(function(e){
this.t = this.title;
this.title = "";
var newImg = new Image();
newImg.src = this.href;
var picheight = '';
var picwidth = '';
for (i=100;i>1;)
{
picheight = newImg.height * (i/100);
picwidth = newImg.width * (i/100);
i=i-3;
if (picheight < 700 && 500 < maxWidth)
i=1;
}
var c = (this.t != "") ? "<br/>" + this.t : "";
if (picwidth > 0){
$("body").append("<p id='preview'><img width="+picwidth+" src='"+ this.href +"' alt='No Image' />"+ c +"</p>");
$("#preview")
.css("top",xOffset + "px")
.css("left",yOffset + "px")
.fadeIn("fast");
}
},
Eddie 3 Jun, 2009
for (i=100;i>1;)
{
picheight = newImg.height * (i/100);
picwidth = newImg.width * (i/100);
i=i-3;
if (picheight < 700 && picheight < 500)
i=1;
}
and the problem is: on mouseover of image link, first time - image is not loaded, pop-up is empty, why?; second time - works fine.
Conrad 6 Jun, 2009
Great little script. Was wondering if it is possible to add a graceful fadeIn/fadeOut effect when hovering over a link?
Mottie 11 Jun, 2009
I love this script, but I needed it to replace the cool DHTML tooltip script from Dynamic Drive. I also fixed the tooltip to stay inside the viewport as well as added a few other features which include setting a background color, font color and tooltip width for each tooltip (and any additional CSS you want). I also made it so you can get the tooltip contents from any object on your page (like a div).
It was a little too long to post here so I posted it on a blog I just started. Please be kind, I only code as a hobby :)
http://wowmotty.blogspot.com/2009/06/new-and-improved-jquery-tooltips.html
Krishan Rodrigo 15 Jun, 2009
yans 15 Jun, 2009
matur thankyou maz ....
webmas 15 Jun, 2009
wptidbits 16 Jun, 2009
Katherine 17 Jun, 2009
Muhammad Usman Arshad 20 Jun, 2009
JC 23 Jun, 2009
Rúbel Mujica 25 Jun, 2009
The script for image preview is great, in fact I'm using it in my webpage, but the xOffset and yOffset variables are inverted when setting the position of the div.
The original code has (in the mouseover and mousemove events):
.css("top",(e.pageY - xOffset) + "px") // should use yOffset for the top
.css("left",(e.pageX + yOffset) + "px") // should use xOfset for the left
and should be:
.css("top",(e.pageY - yOffset) + "px")
.css("left",(e.pageX + xOffset) + "px")
Once I changed this, after configuring this two parameters, everything went just fine.
Thanks!
Allen 28 Jun, 2009