Image Zoom Effect With jQuery

Today we will create simple image zoom effect using jQuery. Actually i wrote this code for one of my client site I have converted code into jquery plugin so that we can easily share and add to your sites.
Read: Create Your Own JQuery Plugin tutorial to learn how to create jquery plug-in for absolute beginners.
Image Zoom Effect With jQuery


Lets will create.


<script src="jquery.min.js"></script> 
<script src="imageZoom.js"></script> 
<div class="imgholder"> 
<img src="img/img1.jpg" alt="img1" width="100%" height="100%"> 
<div class="imgholder"> 
<img src="img/img2.jpg" alt="img12" width="100%" height="100%"> 


//imgZoom plug in starts here 
var s= $.extend( { 
zoom: 25, 
speed: 300 
}, options); 

return this.each(function(){ 
//getting current image 
var img = $(this); 
//load function trigger when image load compleate 
img.load(function() { 
//current width and height of the image 
var w=img.width(); 
var h=img.height(); 
//on image hover event 
//incrasing image height and width. 
height: h + s.zoom, 
marginLeft: -(s.zoom/2), 
marginTop: -(s.zoom / 2) 
}, s.speed,'linear'); 
height: h, 
marginLeft: 0, 
marginTop: 0 
}, s.speed,'linear'); 

The plugin code is straight forward.
-After image load in the page we get the height and width of the image.
-when image hovered increasing image height and width with jquery’s animate function.
-When unhover changing the height and width back to normal height and width.
Note: It’s very important to use jQuery.load() function on images. Because DOM load and other resource like script and image load are different in any browsers. So Note this post it may help in feature.


float: left; 
width: 220px; 
height: 318px; 
border: none; 
-moz-border-radius: 5px; 
-webkit-border-radius: 5px; 
border-radius: 5px; 
box-shadow: 0px 0px 2px #999999; 
overflow: hidden; 
padding: 0px; 
margin-left: 30px; 

Add this code to head section of index.html.

Calling the plugin

Now we need to call your plug in inside index.html to apply it on all image on that page.

//simple one 
//if you want to change setting 
var mysettings={ 
zoom: 55, 
speed: 500 

Add this code any where on index.html page.
It’s done now open index.html page in any of your favorite browsers.