SpyreStudios

Web Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

Coding an Image Thumbnail Hover Magnify Effect with jQuery

April 25, 2013 by Jake Rocheleau

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 Codepen.io 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 Demo – Download 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">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>jQuery Image Zoom Viewer Demo</title>
  <link rel="shortcut icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="icon" href="http://spyrestudios.com/favicon.ico">
  <link rel="stylesheet" type="text/css" media="all" href="http://fonts.googleapis.com/css?family=Lato">
  <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>
</head>

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 http://www.flickr.com/photos/rustyangel/4509569191/ -->
    <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");

	$(".magnify").mousemove(function(e){
		//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 Demo – Download 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.

Filed Under: Tutorial Tagged With: css3, image, javascript, jquery, opensource, tuts

Recent Posts

  • 31 Fresh Design Elements for Spring and Easter
  • 10 Templates for Music Concert Flyers
  • How to Build a Web Scraper Using Node.js
  • Best PHP Books, Courses and Tutorials in 2022
  • How to Get Your First Web Design Client

Archives

  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • May 2008
  • April 2008

Categories

  • Accessibility
  • Android
  • Apps
  • Art
  • Article
  • Blogging
  • Books
  • Bootstrap
  • Business
  • CSS
  • Design
  • Development
  • Ecommerce
  • Fireworks
  • Flash
  • Freebies
  • Freelance
  • General
  • Giveaway
  • Graphic Design
  • HTML5
  • Icons
  • Illustrator
  • InDesign
  • Infographics
  • Inspirational
  • Interview
  • Jobs
  • jQuery
  • Learning
  • Logos
  • Matrix
  • Minimalism
  • Mobile
  • Motion Graphics
  • Music
  • News
  • Photoshop
  • PHP
  • Promoted
  • Rails
  • Resources
  • Showcase
  • Tools
  • Tutorial
  • Twitter
  • Typography
  • Uncategorized
  • Usability
  • UX
  • Wallpapers
  • Wireframing
  • WordPress
  • Work

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

SpyreStudios © 2022