
How to: Create a fancy image gallery with JQuery
Javascript is a very good way to create fancy contents and enhance your website functionnality. In this article, let’s have a look to some interesting script dedicated to create stunning tooltips and galleries with the help of Javascript.
Demos:
Installation
Step 1
First, you have to include the JQuery library between the <head> and </head> tags of your html page:
<script type="text/javascript" src="jquery-latest.pack.js"></script>
Step 2
Once you inclued JQuery to your html file, you have to include the following functions. To do so, embed it within <script type=”text/javascript”> //the code here </script>, or even better, put it in a separate .js file.
this.screenshotPreview = function(){
/* CONFIG */
xOffset = 10;
yOffset = 30;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */
$("a.screenshot").hover(function(e){
this.t = this.title;
this.title = "";
var c = (this.t != "") ? "
" + this.t : "";
$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");
$("#screenshot")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
},
function(){
this.title = this.t;
$("#screenshot").remove();
});
$("a.screenshot").mousemove(function(e){
$("#screenshot")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
};
this.imagePreview = function(){
/* CONFIG */
xOffset = 10;
yOffset = 30;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */
$("a.preview").hover(function(e){
this.t = this.title;
this.title = "";
var c = (this.t != "") ? "
" + this.t : "";
$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");
$("#preview")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
},
function(){
this.title = this.t;
$("#preview").remove();
});
$("a.preview").mousemove(function(e){
$("#preview")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
};
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 */
$("a.tooltip").hover(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");
},
function(){
this.title = this.t;
$("#tooltip").remove();
});
$("a.tooltip").mousemove(function(e){
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
};
Step 3
Once you inclued the gallery functions, you have to activate it. Like for the functions, you can embed it on your html page or put it in a separate .js file.
$(document).ready(function(){
tooltip();//active les tooltip simple
imagePreview();//active les tooltip image preview
screenshotPreview();//active les tooltip lien avec preview
});
Step 4
The gallery is now fully functionnal. Though, we had to use some css to style it.
Paste the following code in your css file:
#screenshot{
position:absolute;
border:1px solid #ccc;
background:#333;
padding:5px;
display:none;
color:#fff;
}
#preview{
position:absolute;
border:1px solid #ccc;
background:#333;
padding:5px;
display:none;
color:#fff;
}
#tooltip{
position:absolute;
border:1px solid #333;
background:#f7f5d1;
padding:2px 5px;
color:#333;
display:none;
}
Step 5
Your gallery is now ready to use. Here’s 3 ways to use it:
To create a classic, but cool tooltip on a link, you have to give the html element a tooltip class, as well as a title:
<a href="http://cssglobe.com" class="tooltip" title="Web Standards Magazine">Roll over for tooltip</a>
To create an very fancy image preview, you have to add the preview class to your html element:
<a href="image.jpg" class="preview" title="Great looking landscape">Roll over to preview</a>
To create an image preview with a link to the full size image, you have to add the screenshot class to your html element, and a rel attribute, containing the full size image url as a value:
<a href="http://www.cssglobe.com" class="screenshot" rel="cssg_screenshot.jpg" title="Web Standards Magazine"> Css Globe</a>
This article is a translation from the French blog WebInventif.




41 Comments + Trackbacks
1.31.2009
This is a really handy little script. Jquery is a good choice for something like this because it is a relatively small download. Have you seen dom tool tip? http://www.mojavelinux.com/projects/domtooltip/
It’s similar and has a smaller download footprint, but if your site is already using JQuery, then this script here is perfect.
2.1.2009
Wow this looks really awesome. I checked out the demos and it looks really neat. I’ve been using lightbox which allows on page load up when you click an image, but the previews functionality of this tool take the cake. Thanks for the step by step advice!
2.1.2009
I really like the image tooltip with captions.. Could think of some ways to use that
2.2.2009
Here is a silly question, would that work on Wordpress blogs? (from all I know it was made especially for blogs
Thank you,
Motti
2.2.2009
Adding images to blog posts is an easy way to make the blog look better and also keep people longer at your site.Thanks for the how-to tutorial!
2.2.2009
Really good article! Thanks a lot
2.2.2009
good tutorial!!!
thx!
2.2.2009
Thanks for the tip. I’m happy with my current gallery though, so no switching at the moment.
2.4.2009
Thanks for the great tutorial. I use jquery too, I get my code straight from their library. So this will add some great customization on my code.
2.4.2009
Sweet – I’m not sure what use it would be of on my current blog, but if I ever make a personal blog that’s what I will use
2.4.2009
Wow! thanks for the simple and quick tutorial. It would be very easy to follow.
2.5.2009
I like JQuery very much and it was so well explained too. It’s very useful. Thanks
2.5.2009
Thanks for this tutorial! Really helped alot..
2.6.2009
I was not familiar with JQuery but it seems like a cool application. I will give it a try.
2.6.2009
Great code man, much appreciated. Thanks for sharing
2.8.2009
Thanks for the tutorial. Post made in right time for me, as i was just looking for fancy gallery customization. Got it installed and working perfectly.
2.9.2009
nice work! I love the result.
2.9.2009
Super, i love JQuery library and what you can do with it, this article is very helpful thank you ones more, i guess i have to bookmark your blog your doing excellent job!
Kind Regards,
Vasilis, UnitedWorx
2.12.2009
Thanks for the awesome tip and code. jquery has grown big over the years. Recently I incorporated the jquery date picker into one of my self developed wordpress plugins. The styles are awesome…
Will try out this one
2.17.2009
Wow these look great. I really like the link tooltip with preview!
2.18.2009
I absolutely love jquery. It is amazingly easy to implement, even for someone with basic coding knowledge. It has become a life saver for sure.
2.19.2009
That looks like Greek to me! Backend has never been quite my speciality, and things like this are best left to the professionals…..hahahaha, just kidding folks. We can all become experts eventually by heeding this and similar advise that is so generously served up by individuals like Jean-Baptiste!
3.2.2009
Nice find Jean. It looks amazing and I’ll definitely give it a try, seems simple enough.
3.2.2009
I didn’t know JQuery before. It seems to be a good program. Your tutorial is well explained too.
3.4.2009
I have used this plugin for a project, but had broken his head slightly to get the image displayed on the left cursor hand.
3.8.2009
Excellent script. And excellent step by step tutorial on how to use the script.
3.8.2009
great coding and nice effort but the DEMO links work or i have a problem ?
3.18.2009
Great article and well explained tutorial
I really liked the demo.
3.23.2009
Excellent explanation with a nice demo.
12.3.2009
I like demo easy for me but one problem ajax pagination without refresh inside div. It will not work with image pop. Any luck with script? Thanks.
1.27.2010
Very intersting information. I think that I can use this on my site . Thanks !!!