written on:8 May, 2008 by Alen Grakalic
Easiest Tooltip and Image Preview Using jQuery
Delicious | Digg | Stumble | Reddit | Float | Subscribe to RSS Feed
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>
Download all 3 tricks in a bundle
About the author:
Designer, developer and a passionate standardista with large experience in all types of front-end work. Started to get involved with web in 1999. and turned freelance in 2005., the same year he started Css Globe. Alen's work has been featured on numerous css galleries including famous Css Zen Garden official list. Available for contract work.
Enjoyed the article?
Subscribe to our RSS feed or share it with your friends.
Delicious | Digg | Stumble | Reddit | Float | Subscribe to RSS Feed
Comments
Subscribe
Join 5000+ subscribers and receive our content instantly.
Follow us on Twitter
Want to be the first to know when a new article is published?
Sponsors

Sitegrinder 2 Photoshop Plugin

Look Professional with FreshBooks
Recent Articles
- CSS do's and dont's Part 2- Markup
- Easiest Tooltip and Image Preview Using jQuery
- 4 Uber Cool Css Techniques For Links
- CSS do's and dont's Part 1: CSS Selecting
- Easy Scroll: Accessible Content Scroller






David Appleyard on 8 May, 2008 wrote:
Steph on 8 May, 2008 wrote:
Boris on 8 May, 2008 wrote:
YellowLlama on 9 May, 2008 wrote:
Janko on 9 May, 2008 wrote:
Ivan on 9 May, 2008 wrote:
Dragon on 10 May, 2008 wrote:
Josh Darville on 10 May, 2008 wrote:
www.TheMediaZoo.com
my name is Josh
JoshD@themediazoo.com
Subash on 11 May, 2008 wrote:
Thanks Alen.
webpixelkonsum on 11 May, 2008 wrote:
Thank you for your nice work.
Ralph
AndreiZ on 11 May, 2008 wrote:
Senko on 12 May, 2008 wrote:
http://blog.senko.net/static/jquery.csstooltip.js
Osvaldo on 13 May, 2008 wrote:
Juan on 14 May, 2008 wrote:
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 on 16 May, 2008 wrote:
snetcher on 17 May, 2008 wrote:
ana gomez on 20 May, 2008 wrote:
payastr on 22 May, 2008 wrote:
Andrew Cornett on 22 May, 2008 wrote:
Vlad on 26 May, 2008 wrote:
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 on 28 May, 2008 wrote:
Thanks
limitless86 on 29 May, 2008 wrote:
Masey on 29 May, 2008 wrote:
Any help here would be greatly appreciated.
intelligenc on 1 Jun, 2008 wrote:
Charles on 3 Jun, 2008 wrote:
Is there a way to simply show the content of a hidden div in this tooltip ?
Thank you
Charles
Cameron on 9 Jun, 2008 wrote:
Dalain on 10 Jun, 2008 wrote:
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 on 14 Jun, 2008 wrote:
Sun on 14 Jun, 2008 wrote:
Nice code!I like it very much.
Sorry for my poor English .
Alex on 19 Jun, 2008 wrote:
Thank you, Thank you, Thank you!!
Dave on 20 Jun, 2008 wrote:
Muneef Hameed on 20 Jun, 2008 wrote:
i like it!
Olivia Bell on 21 Jun, 2008 wrote:
Ravi on 21 Jun, 2008 wrote:
Ravi.
Abayomi Oyewumi on 23 Jun, 2008 wrote:
u just added to a life
greeeeeeeeeee
Albano on 24 Jun, 2008 wrote:
Fred Riley on 27 Jun, 2008 wrote:
Home
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 on 4 Jul, 2008 wrote:
http://jim-murray.ca/shows.html
Just hover over some of the underlined shows.
Enrique Espinosa on 8 Jul, 2008 wrote:
LJ on 8 Jul, 2008 wrote:
cyberbuff on 10 Jul, 2008 wrote:
James Breen on 10 Jul, 2008 wrote:
http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fcssglobe.com%2Flab%2Ftooltip%2F01%2F
Thanks
Jim
Mark Abucayon on 13 Jul, 2008 wrote:
gnysek on 13 Jul, 2008 wrote:
I don't belive that it's so easy!
Alex on 15 Jul, 2008 wrote:
$("#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 on 17 Jul, 2008 wrote:
Clean implementation too!
Toxane on 20 Jul, 2008 wrote:
Em on 21 Jul, 2008 wrote:
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 on 23 Jul, 2008 wrote:
steve on 23 Jul, 2008 wrote: