zoom-droplet

I’ve recently installed Fancybox (my new favourite lightbox plugin for jQuery) so you guys can zoom in on selected images on the blog rather than squinting and trying to view images that are only 448px wide. The only problem was, it wasn’t obvious that you could click an image to zoom in on it.

So I thought it’d be nice that when you roll over an image that you could zoom in on, a magnifying glass icon would appear on top of the image indicating you can zoom in. I did a few searches first to check that I wasn’t re-inventing the wheel but had no luck, I’m sure there is something similar or better out there though, if there is let me know.

An example of what the code does

Go ahead, roll over the image, see the magnifying glass, then click the image to zoom in. Please remember, my code doesn’t do the image zooming, Fancybox does.

large-droplet

The jQuery code

This is the full code, but I suggest you read on for an explanation. If you just want to get this to work on your own site, change the img src (on line 7) to your own magnifying glass image, add this around your images : <a class="zoom">...image code...</a> and make sure you’ve got the jQuery code below on your page somewhere.

var loadGlass = 1;
 
$('a.zoom img').hover(function(){
 
	if(loadGlass == 1) {
 
		$('body').prepend('<img id="magnify" style="position: absolute;" src="http://www.nouveller.com/wordpress/wp-content/themes/maroon-moon/images/icons/magnify.png" alt="" width="32" height="32" />');
 
	}
 
	var p = $(this);
 
	var position = p.position();
 
	$('img#magnify').css({'top' : position.top+20, 'left' : position.left+20});
 
	$('img#magnify').show();
 
},function(){
 
	$('img#magnify').hide();
 
	loadGlass = 2;
 
});

The explanation

Right first we create a variable. This will make sure that we don’t load the magnifying image onto the page more than once as we use jQuery to load the image on line 7.

var loadGlass = 1;

Now we create a function that will activate when we hover over an image that has a link around it with the class of zoom

$('a.zoom img').hover(function(){

If the loadGlass variable is equal to 1 we will load the magnifying image.

	if(loadGlass == 1) {
 
		$('body').prepend('<img id="magnify" style="position: absolute;" src="http://www.nouveller.com/wordpress/wp-content/themes/maroon-moon/images/icons/magnify.png" alt="" width="32" height="32" />');
 
	}

Then we get the position of the image using these two variables.

	var p = $(this);
 
	var position = p.position();

All that’s left for the roll over to do is to position the magnifying image 20 pixels more the than rolled over image’s y position and 20 pixels more than it’s x position, then show it, if it’s been previously loaded and hidden.

	$('img#magnify').css({'top' : position.top+20, 'left' : position.left+20});
 
	$('img#magnify').show();

Lastly we set a function to run for when you roll off the image. We hide the maginifying image and then set loadGlass to 2 so that the if statement on line 5 won’t load the image again. As the image is a transparent PNG it will add several images over the top of each other causing the transparent black to become solid.

},function(){
 
	$('img#magnify').hide();
 
	loadGlass = 2;
 
});

Author: Benjamin Reid

My name is Benjamin Reid, I live in the UK and keep the magic locked into this site.

RSS Icon

Subscribe to the RSS feed some design goodness, get in on the upcoming software give-aways!

Digg Icon Share How to add a magnifying glass to your lightbox enabled images to Twitter Share How to add a magnifying glass to your lightbox enabled images to Delicious Share How to add a magnifying glass to your lightbox enabled images to Facebook

Related post(s)

Comments left by other wizards

28 Responses | Make a comment

Leave your words of wisdom

Leave a Reply

This is a Gravatar

Your name