The ‘imgPreview’ plugin allows your users to preview an image before clicking on it and, out of necessity, will preload the image so when a user does click through to it there is no waiting time!
The image preview shows up in a tooltip-like box appearing alongside the user’s cursor when hovering over a link. The plugin is entirely unobtrusive; it does not require any hooks to target specific links (no non-semantic classes); it will automatically detect the anchors that are linking to images and will only apply the preview effect to them.
Options
Additionally, it allows for quite a high level of customisation, the following options are available: (to see a demo, scroll down to the bottom of the post)
- imgCSS – This setting must be an object. The CSS specified within will be applied to the preview image. So, if you wanted to limit all image previews to a width of 200px you could do it like this:
$('a').imgPreview({
imgCSS: {
width: '200px'
}
}); |
- distanceFromCursor – This setting must be an object. Within it there must be a ‘top’ and ‘left’ property, both of which correspond to position of the preview in relation to the user’s cursor. By default the position is set to
{top:10,left:10}
. Be sure not to position the preview directly over the link as this will mess up the hover in/out effect; an offset is required.
- preloadImages – This setting must be a Boolean value (true/false). This setting dictates whether or not the plugin will preload the images before the user hovers over any particular link. This is recommended and is set to true by default – if you have a lot (>30) of links to images on a single page then it’s advisable to disable this feature.
- Callback functions:
- onShow – The onShow setting must be a function. It is a callback which will be run when the preview container appears. Within the callback you have access to the preview ‘container’ via the
this
keyword and the hovered link via the first parameter.
- onLoad – The onLoad setting must be a function. It is a callback which will be run when the preview image has loaded. It’s different to onShow in that it will only run once the image has been loaded, not before. Within the callback you have access to the preview image via the
this
keyword and the hovered link via the first parameter. To get access to the container you could just do: $(this).parent()
.
- onHide – The onHide setting must be a function. It is a callback which will be run when the preview container disappears (when a user stops hovering over a link). Within the callback you have access to the preview ‘container’ via the
this
keyword and the hovered link via the first parameter.
- containerID – This setting must be a String value and will determine the ID applied to the preview container. This is not for the plugin’s benefit; its only purpose is to accomodate CSS styling (within a stylesheet).
- containerLoadingClass – This setting must be a String value and will determine the class added to the preview container when an image is loading and will be removed when the image has finished loading. If you’ve enabled preloading of images then don’t worry about this setting as it won’t have any effect.
- thumbPrefix [NEW in V0.13] – This setting allows you to specify a thumbnail prefix for images. So, if you have an anchor with an href of ‘images/whatever.jpg’ and you have set this setting to ‘thumb_’ the preview-image will end up as ‘images/thumb_whatever.jpg’.
- srcAttr [NEW in V0.2] – This setting allows you to specify what attribute to retrieve the image SRC from. So, if you have a link to a website and you want to show a screenshot of that website then you can store the location of the preview image in the ‘rel’ attribute of the anchor, leaving the ‘href’ untouched. E.g.
$('a').imgPreview({srcAttr: 'rel'});
. By default this setting is set to ‘href’. Along with this new option it is now possible to get an image preview when hovering over non-anchor elements – you must specify the right ‘srcAttr’ though.
Download
Demo
Various demonstrations of the plugin can be found over here: https://j11y.io/demos/imgPreview/full/
As always, please let me know about any bugs, concerns or questions in the comments below! 🙂
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
Wow this is dope. Without looking at the code, could this be extended for say iframes or just other dom elements?
Cool, nice work James really easy to implement on any project 🙂
Wow, so simple idea with a perfect effect to the viewer.
On next version adjust preview positioning to be visible within the visible part of the browser window (i.e. if the window is scrolled such that the links are at the bottom, the preview will not fully appear).
Looks like a great plugin. Nice job.
As Yasser says it would be nice if the preview appeared above the link when the link is near the bottom of the window.
Effective with refined style ….
TKX great work
You have one of the neatest sites around and this is a cool plugin you’ve developed. I’ll use it some time soon on my site I guess ;o)
Thanks for all the comments!
@Patternhead, @Yasser, good idea! I’ll try integrating that in the next release.
@Joe, yep, that would definitely be possible, I might release something of that nature in the future.
Good stuff James – the code and explanation are clear. However, on a purely personal note, I’m not a fan of the end-result.
I particularly dislike those ‘web page preview’ popup thingies (technical term) that are of the same ilk.
One could argue that a thumbnail is visually unobtrusive anyway, and the preview aspect is rather pointless.
I’m saying this in the nicest, polite way 🙂
Dosen’t load in Opera 9
@pineappleclock, sorry about that, I just fixed it so it should work now. 🙂
@Shane, I know what you mean, but I still think, in certain situations it can really improve the usability of a site. I’m not saying it acts as a substitute for an actual preview but sometimes it just makes more sense – for instance, when there’s limited space on a page…
This will work for smaller images, but it might take some time to load larger images. Perhaps you should support an optional thumbnail?
@Anon, great suggestion! I’ve just added it as a new setting,
thumbPrefix
(added to v0.13) – you can specify a prefix for images such as ‘thumb_’.Great work.
Thanks!
Hi there James,
Very nice plugin! Added a release notice here:
http://www.opensourcereleasefeed.com/release/show/jquery-imgpreview-plugin
Please feel free to add all future update releases there as well.
All the best,
Schalk Neethling
really great!
Muito bom o pluguin! 😀
When I click the link it takes me to the image. Is it what you intend to do? I am not sure. For progressive enhancement point of view I would suggest the following. The images are shown if js is not active. If js is active the image will be hiden and a link will be added for tooltips. And the link will be disabled by preventDefault().
Thanks for the comments!
@Schalk, awesome, thank you.
@Shin, I’m not sure you understand the purpose of an “image preview”. With progressive enhancement as the objective the plugin is only a means to view images before clicking through to them. The scenario you describe would result in diminishing usability – you’re removing images from view – that will confuse a user.
that’s hot! thanks I like it.
Hey nice one. This is going to make browsing a shopping cart I’m working on a whole lot more snappy.
I like Shin’s comment…. I am using the script as a way to show a preview of a link…meaning, i’m showing a screenshot of the page, and, if there is a link (that isn’t #) clicking on the link will bring them to the web page…does that make sense? Any thoughts on how that could be done?
Second question…for some reason the only way my positioning works is with it linking to:
https://j11y.io/demos/imgPreview/styles.css
Even if I copy hte styles for: imgPreviewContainer, imgPreviewContainer span, and imgPreviewContainer.loading into my own CSS, and link to it, it breaks and loads the prewview at the very bottom of the page:
ie: http://www.lukemv.com/clients.php
Very nice plugin. could also use some lightbox plug-in to handle the click event and maybe have the hover show the image like it is now and onclick have the full image in a lightbox?
there should me some provision for onClick event aswell
mmmm… i cant get this to work: http://tinyurl.com/da89hr
what did i miss??
Thanks!
mmm… scratch that – looks fine now. Thanks again for this!
(PS feel free to delete these 2 posts…)
Right, I’ve just released version 0.2. It fixes a couple of bugs and adds a new option: “srcAttr” – This new option allows you to specify what attribute to retrieve the image SRC from. So, if you have a link to a website and you want to show a screenshot of that website then you can store the location of the preview image in the ‘rel’ attribute of the anchor, leaving the ‘href’ untouched. E.g.
The above would work with links like this:
You can download 0.2 here: http://plugins.jquery.com/files/imgpreview.full.jquery.js.txt
This is excellent.
Only thing missing now, is to shift the preview left/up when it happens to be at the edge of the window
Thanks again!
Awesome, thanks!!
I’m using it here http://www.barbaragerardmitchell.com/ Thx again James this is really nice.
looks interesting on image maps too:
http://arkadien.org/southerneurope_jquery.html
Hey there, great script. However I’ve discovered that it does not seem to work in IE 6 or 7 at all. It throws up a nice “Operation Aborted” error. I am using jquery 1.3.1 and the latest version of imgPreview.
Hey thanks a lot James. This is a really awesome script!
I can confirm Benji’s comment. Images do not seem to load in IE6 or IE7. Images _will_ load in Firefox 2, but the nice rounded corners do not appear.
Amazing piece of work otherwise.
James, thank you. Seriously, thank you for your time and your talent. People like you who are willing to spend the time to teach others is very rare! I can’t wait to see your future posts.
I’ve loved it! Thanks!
This is superb James – exactly what I was looking for – Thanks!
Nice work! Thanks!
One suggestion: it fails with complex urls to the images, such as “http://myimage.gif?h=234” because it looks for image filetype suffixes at the end. I’d recommend either a braindead approach of looking for image suffixes anywhere in the link or getting a good URI processing regex included so you can tease out just the filename. (Too bad jquery doesn’t have one built in!)
Nice work!! just one question. How can I use it whith php/mysql? I use to open the picture in another window using this:
<a href="javascript:Abrir_ventana('ver.php?NOMB=&db=');" rel="nofollow">
that’s so nice, thank you so much for this.
but i was wondering that, what if i want to use it inside my wordpress posts to get the the image from custom values instead.
let me give you example, in my wordpress i have thumbs in all posts and i want to use imgPreview to a preview these thumb to a little bigger.
so what i should do? and can i use this function to preview the image or i cant do that?
thank you again 🙂
Tres sympathique ces petits plugins !! MErci
Great work on the plugin.. but i have a problem using it.. I cant seem to load images using querystring parameters(which i usually use to get Dynamically created images).. any URL to an image which contains the ‘?’ symbol does not show up in the popup..seems like preloading doesn’t work on these kind of images.. please correct me if i am missing anything.
This is so great, really great… I was searching for a plugin like this but took long time until I found it… Thank you
Hi could you publish or send me a zip file containing the demo page?… The problem is I’m from Cuba and google has blocked this country, so, anything from the googleapis doesn’t work for us.
That’s great . Super and Thanks…
Very cool plugin without a doubt has several utilities
hey, great script!
Is it possible to have the image preview open to the left of the cursor?
i need it to act exactly opposite to what it is now for a RTL website…
I’ve tried to change all the “left” into “right” and the “+” into “-” in all sorts of different variations inside the .js file, but with no success..:(
Any ideas anyone???
It’s a nice plugin, I liked it, but it is have some bug:
If you will put «» before «» IE doesn’t want to open the page, and it seems that a problem is here: $container = $(”)….. in JS file.
O.k. now if i change this section of code:
left:a.pageX+b.distanceFromCursor.left+'px'
into this:
left:a.pageX-f.width()-50+'px'
It’s working correctly only on FF, IE 6,7 do not display it correctly
any ideas?
please help!
BTW i use the 0.22 version, does anyone happens to have the full code? (not the minified one)
i could only find v0.21..
For those of you having problems with IE6 / IE7 I found the issue lied with wanting to perform some animation calls. The following works just fine for me.
Should $(‘.some-not-existing-class a’).imgPreview(); work? IMO it should, as this syntax works in jQuery itself without a problem. This example throws an error (tested in IE8 and FF3). Maybe a bug?
Workaround:
$(‘.some-not-existing-class a’).each(function() { $(this).imgPreview(); });
Thanks for this nice plugin anyway. 🙂
Hello and thanks for the great plugin. My programmer and I are working on http://test.greenbarnes.co.uk and we’re using your plugin. Its working a treat and we’re pleased with it but struggling to keep the images contained on a 1024×768 screen. Is there any way we can make the images stay within the browser window when they want to appear outside of it? This is mentioned by Moche also on February 8, 2009. Thanks, Pete.
This plugin would go great with BitPixels: see http://www.bitpixels.com/getthumbnail?code=91327&size=200&url=james.padolsey.com
(Or to fool your code into working: http://www.bitpixels.com/getthumbnail?code=91327&size=200&url=james.padolsey.com&type=.jpg)
Your very own snap.com link preview (without the ads)
Anyone know how to make this work using classes instead of ids to trigger the preview? ie.: class=”thumbs” instead of id=”thumbs”. I need to use this multiple times on a page and using the same id more than once is technically wrong and causes my validation software to report errors. For clean coding I’d like to try to fix this.
I have a simple implementation of the script at
http://www.artglassusa.com/pages/glass_vessels.html
I am using the image preview with jquery library 1.3.2-min…I keep getting an error: G is undefined in the jquery library every time i hover over my image and then again when i leave the image…any ideas guys???
i m using preview image plugin in jquery 1.3.2min but i have not find the solution. the problem is that my preview is shown on the end then my web page is scroll down or form the right side
is there any way to shown image above instead to showing downward
Asad,
Just set the distanceFromCursor +/- # of pixels as per the demo. I use two difference ids so previews on the left can show to the right and previews on the right can appear on the left of the cursor.
I have problems with IE6. It brakes with an alert an says “operation aborted”. Any ideas? Thanks Kai
I don’t remember the exact error I was getting, but make sure you don’t have any background colors defined with names. ie. color:White. Instead you should use color:#FFFFFF.
This fixed the IE errors for me, hopefully this will do it for you too.
Hi Guys,
Ill try again and ask about an issue im having…Im using a jquery library 1.3.2-min and trying to get the imgpreview working.
It works fine and displays the images correctly however everytime the imgpreview is shown and hidden, a javascript error is spat out…
The error is coming from the jquery library in the form of: “G is undefined”.
Any of you come across anything like this???
Cheers guys
Hi,
Plug-in is very cool.
But I have one problem is that the image preview is not shown inside the web page when I mouse over on it, where the thumbnail is placed at the end of the page.
hi everyone, just wondering if this works in wordpress. does anyone know? thanks.
josh,
1. Download the 2 file: jquery.min.js and imgpreview.min.0.22.jquery.js(or imgpreview.full.0.22.jquery.js)
2. Upload to [wp-includesjsjquery]dir
3. Edit header.php and footer.php of the theme that you use.
[header.php]
Add the next lines to the line upper than </head>.
<script src=”http://www.example.com/wordpress/wp-includes/js/jquery/jquery.min.js” type=”text/javascript”></script>
<style type=”text/css”>
<!–
/* here is the css code of demo’s page */
–>
</style>
[footer.php]
Add the next lines to the line upper than </body>.
<script src=”http://www.example.com/wordpress/wp-includes/js/jquery/imgpreview.min.0.22.jquery.js” type=”text/javascript”></script>
<script type=”text/javascript”>
//<![CDATA[
// here is the js code of demo’s page
exp.(Regular, with limited width:)
$(‘ul#second a’).imgPreview({
imgCSS: { width: 200 }
});
//]]>
</script>
4. When you post an article, Add id=”second” to <ul> element. And link from a text or an image to an image displaying popup.
exp.
<ul id=”second”>
<a href=”http://www.example.com/wordpress/wp-content/uploads/2009/08/sample.gif”><img src=”http://www.exapmle.com/wordpress/wp-content/uploads/2009/08/thum.gif” alt=”sample” title=”sample” width=”**” height=”**” class=”size-full” /></a>
</ul>
Please try it.
Hi James! I am trying to use your plugin, bud I cant get it work. I am still having error “a is undefined” in minified version and “src is undefined” in normal. Nothing happens. This is what i am doing:
include jQuery 1.3.2
include imgpreview
HTML
<a class=”test” href=”/images/fotos/praha/6-4_big.jpg”>test</a>
Javascript
$(‘a.test’).imgPreview();
Its not working. Any ideas what I am doing wrong?
Love this plug in! Easy to implement and seems ideal for the project I’m working on. Only question is, is there anyway to make the tool tip trigger when someone mouses over a TD not just the link within the TD? I’m using some other jQuery to make the whole TD a link and would like the preview to show for the whole thing as well. Also, can the preview box be set to be a fixed position instead of relative to the cursor?
Chates,
It doesn’t work unless I write Javascript below than the link a tag of the image.
exp.
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″>
<title>TEST img Preview</title>
<script src=”jquery-1.3.2.js” type=”text/javascript”></script>
<script src=”imgpreview.min.0.22.jquery.js” type=”text/javascript”></script>
</head>
<body>
<h1>TEST</h1>
<h2>jQuery plugin – imgPreview -</h2>
<div>
<a class=”test” href=”/images/fotos/praha/6-4_big.jpg”>test</a>
</div>
<script type=”text/javascript”>
$(‘a.test’).imgPreview();
</script>
</body>
</html>
The first 4 examples in the demo works fine for me.
I’m having problems only when I attempt to preview a link to a website. When I do this, all I see is a place holder…. i.e. there is no preview, just the animated image loading gif…
Website links + previews
Google
Yahoo
Amazon
Anyone have any recommendations?
Thanks!
This is such a great plugin, thanks for creating it.
As others have mentioned, PLEASE implement a fix for when the image previews are close to the bottom or right of the screen.
Thanks again for making such a killer plugin!
My website is an intranet site…
Most of my web links are to other intranet URL’s.
In my case, preview images for these intranet URL’s do not already exist.
I confirmed that the various free preview image services available on the net can’t create a preview image for an intranet site.
I’d like to know if this plugin can be modified to create it’s own URL preview images “on the fly”?
If not, can anyone recommend a different JQuery plugin that can create intranet URL Preview images?
Thanks!
I have 2 solutions for the “a is undefined” error. The first one was explained earlier, you must put your
$('.pic a').imgPreview()
after the image, but a better way is to put it in the$(document).ready()
likeAnother possibility is that if there is no images (
li
) in yourul
, there will also be an error. Maybe this should be considered a bug?it very nice! I’ll use it in my website: http://www.77street.net.
thinks.
I also have the issue that if an element doesn’t exist it throws an error. I think the plugin should do a search and only initiate if the element is present.
Hopefully this will be fixed in the next version.
Couple of issues with this plugin. The image dissappering issue in IEs when using animations I don’t have a fix for, but with regards to the plugin failing and killing javascript in general when selectors return an empty result set, you just need to add
After this bit of code in the original file:
so that it’ll look like this:
Now you can do
and not kill your js chain if the class doesn’t exist or no srcAttr hits are found inside.
Hi James,
I am Icelandic photographer living now in China. I like your great plugin a lot. I am now working on this heavy idea where I use a big clickable map, which is so far working great:
http://www.photo.is/china/index.html
But I have 2 problems:
1) Is it possible to let the picture run around the cursor, like have the picture on the left side when the cursor is on the right side of the screen …?
2) Is it possible to add different type of icon/logo over the picture by some selection?