Howdy stranger! Wanna join? (or login here)
written on:8 May, 2008 by Alen Grakalic
Share on Facebook Tweet this Delicious StumbleUpon Reddit Subscribe
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
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.
The script takes a title attribute of an A tag and place it inside the popup element.
Html looks like this:
<a class="cssglobe.com" class="tooltip" title="Web Standards Magazine">Roll over for tooltip</a>
View this script in action
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 class="1.jpg" class="preview"><img src="/img/spacer.gif">or if you want to use caption add a title attribute:
<a class="image.jpg" class="preview" title="Great looking landscape">Roll over to preview</a>View this script in action
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 class="www.cssglobe.com" class="screenshot" rel="cssg_screenshot.jpg">Css Globe</a>again, if you wish to use caption add a title attribute:
<a class="www.cssglobe.com" class="screenshot" rel="cssg_screenshot.jpg" title="Web Standards Magazine"> Css Globe</a>View this script in action
Download all 3 tricks in a bundle
Designer, developer and a passionate standardista. Long time web professional with huge experience in all types of front-end work. Founder of Css Globe and creator of Easy front-end framework.
To get in touch, visit Alen's personal page, follow Alen on Twitter or become a Facebook friend.
Then don't miss our next one! Subscribe to our RSS feed or share it with your friends.
Share on Facebook Tweet this Delicious StumbleUpon Reddit Subscribe
View All Articles
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
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 class="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
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
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): 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: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 :
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