Coding an Image Thumbnail Hover Magnify Effect with jQuery

I have run into a number of simple effects to generate image magnification. You can sometimes find this on e-commerce websites for product images when you hover onto the thumbnail and a bigger zoom effect will display. This is a great technique no matter what type of website you have. But I was running into a lot of trouble with plugins, and then I managed to find this excellent tutorial on The Code Player.

All of the source codes are released in a repository on which also has a lot of follow-up discussion. I was very impressed with the codes and I wanted to organize everything a bit better. So in this tutorial I want to demonstrate how you may customize this basic JS code to work for any image on your website. The jQuery is structured for targeting a specific class of images on the page and then displaying the full view when hovered. I’ll explain the most important bits in greater detail.

magnifying glass zoom image hover effect jquery screenshot

Live DemoDownload Source Code

Structure the Webpage

We do not need to worry about much HTML in the layout. I am using a standard HTML5 doctype along with a local copy of jQuery 1.9.1. Also I’ve taken the time to copy over all of the JS code into a new file called magnify.js. There should not be many areas which require editing so it helps to organize everything separately.

<!doctype html>
<html lang="en-US">
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>jQuery Image Zoom Viewer Demo</title>
  <link rel="shortcut icon" href="">
  <link rel="icon" href="">
  <link rel="stylesheet" type="text/css" media="all" href="">
  <link rel="stylesheet" type="text/css" media="all" href="style.css">
  <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
  <script type="text/javascript" src="js/magnify.js"></script>

The image area block is encapsulated inside a div with the class .magnify. You’ll find an empty div area using the class .large which contains the magnifier object. All of the width/height features are created in CSS which looks a lot better in almost every browser. Then I have renamed the image class to .thumb so it is easier to recognize.

  <div class="magnify">
    <div class="large"></div><!-- This is the magnifying glass which will contain the original/large version -->

    <!-- Image source -->
    <div class="imgbox"><img src="images/fullsize/01-psx-controller.jpg" width="600" height="398" class="thumb" /></div>

  </div><!-- @end .magnify -->

This is probably the most important code in your document since it all requires some effects via jQuery. And the selector objects will be looking for these exact classes. The only custom addition .imgbox is a container I added onto the image to keep it centered within the layout. It is also important to recognize I am linking directly to the fullsize image in the src=”” value. This is crucial because the hover effect only works if we have a full-scale image resized via width and height attributes.

Design Styles

To fit the main container I am using margin: 0 auto along with a custom background tile from Subtle Patterns. Now the internal image is using a series of box shadows which are not visible in the magnifying glass. This is a fairly straightforward process and since we are pointing to the same URL in the image file, it makes a lot more sense.

/** images **/
.imgbox {
  display: block; 
  width: 100%;
  text-align: center;
  margin-bottom: 40px;

.imgbox img {
  -webkit-box-shadow: 1px 4px 9px -1px rgba(0,0,0,0.65);
  -moz-box-shadow: 1px 4px 9px -1px rgba(0,0,0,0.65);
  box-shadow: 1px 4px 9px -1px rgba(0,0,0,0.65);

.credits { text-align: center; margin-bottom: 20px; }

.magnify {width: 600px; margin: 50px auto; position: relative; cursor: none;}

/* Let's create the magnifying glass */
.large {
	display: none;
	width: 175px; 
	height: 175px;
	position: absolute;
	-webkit-border-radius: 100%;
	-moz-border-radius: 100%;
	border-radius: 100%;
	/* box shadows to achieve the glass effect */
	-webkit-box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
	-moz-box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25);
	box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.85), 0 0 7px 7px rgba(0, 0, 0, 0.25), inset 0 0 40px 2px rgba(0, 0, 0, 0.25)

One alternative would be to provide an HTML5 data attribute into the image, and then target this value in JavaScript. This way you could be able to display a smaller thumbnail image on the page while still linking to the full-screen image using something like data-fullsize-src=””. But of course, this requires resizing the background image to fit within the thumbnail view. It is not the easiest code to digest but the script works exactly as you would expect.

Implement with JavaScript

It is worth noting how well-written the entire set of functions appears to be. You can find comments at almost every line in the code, so even JavaScript newbies will not be scouring for questionable blocks of code. Those who are unfamiliar with JS will not need to change anything. Just remember that the .thumb class is what you need to apply onto your default image, along with an outer container of .magnify.

  var native_width = 0;
  var native_height = 0;
  $(".large").css("background","url('" + $(".thumb").attr("src") + "') no-repeat");

		//When the user hovers on the image, the script will first calculate
		//the native dimensions if they don't exist. Only after the native dimensions
		//are available, the script will show the zoomed version.
		if(!native_width && !native_height)
			//This will create a new image object with the same image as that in .small
			//We cannot directly get the dimensions from .small because of the 
			//width specified to 200px in the html. To get the actual dimensions we have
			//created this image object.
			var image_object = new Image();
			image_object.src = $(".thumb").attr("src");
			//This code is wrapped in the .load function which is important.
			//width and height of the object would return 0 if accessed before 
			//the image gets loaded.
			native_width = image_object.width;
			native_height = image_object.height;

I am using an updated version of the JavaScript codes which will automatically pull out the full-scale image without resizing. This means we only need to add one URL value into the src attribute. There are lots of code snippets with jQuery selectors referencing this .thumb class. I find it important that developers should be able to update this class to suit their own website layout.

But when you change this class it requires a couple updates to the JavaScript. You will just need to search for lines directed at $(“.thumb”) and change the value to whatever class name you need. There are only about 4 or 5 different instances so the Find & Replace tools within any popular IDE should do the trick. I certainly have not had any problems with this script and it should not take much effort to get it working with your own images. Of course, you can’t use partial thumbnails because then users wouldn’t be able to see the whole image when hovering(which is the whole point of magnifying).

But you can get a better idea from checking out my live sample demo below. To reiterate again these are not my own codes, but were originally published elsewhere in a related tutorial by Ruby on Tails. All of his codes are released as free and open source for other developers. I simply took this base and configured it to work in a nice layout, along with any typical images you choose. But please take a peek at my sample demo and download a copy of the project codes if you want to try out your own custom edits.

image photo zoom fullscale open source jquery tutorial

Live DemoDownload Source Code

Final Thoughts

I did go through at least 3 different jQuery magnify hover plugins before finding this gem in Codepen. I feel the magnification effect is not given enough credit in modern web design. And taking user experience into deep consideration, this effect is worth implementation when it can improve your overall website style. Please do check out my live sample demo or visit the main tutorial page if you want to research a bit deeper.