Thursday, March 18, 2010

Dynamic Popup Image Zoom with Magnifying Glass Effect using jQuery

Lot of times on your webpage you might want to include an image of a very large resolution but the users then have to open that image in a new window to view its details in actual resolution. In situations like that, comes in handy some kind of image zoom tool which can display the actual image resolution/details just by hovering your mouse over that. These kind of image popup zoom boxes can be seen on some merchant websites where they don’t want the customers to navigate away from their product page.

      I have implemented a very easy to use image zoom tool where the popup appears just next to the image and the zoomed image pans based upon the mouse position on the original image giving the magnifying glass effect. This tool works with images of any resolution and aspect ratio. The zoom popup box width can be adjusted using the JavaScript and the height is automatically adjusted as per the aspect ratio of the original image.

Why should you use this image zoom tool:

  1. Zero coding required.
  2. No strict HTML layout/structure needed to place the images in the document.
  3. Works with images of any resolution and aspect ratio.
  4. Just by modifying the jQuery selectors in $(document).ready() function you can assign zoom property to the images on your page. For example: to assign zoom effect to all the images on your page, just use $("img").mouseover() selector.
  5. Zoom area width can be easily adjusted using the JavaScript zoom_width variable and the height is adjusted automatically based upon the image aspect ratio.
  6. There is not need to have a separate thumbnail for the images to be zoomed.

Here are a few examples of how the zooming looks like:Dynamic Popup Image Zoom with Magnifying Glass Effect Dynamic Popup Image Zoom with Magnifying Glass EffectDynamic Popup Image Zoom with Magnifying Glass Effect

The JavaScript code is very easy to incorporate and robust enough to handle any kind/size of images. You can use your own CSS/HTML to place the pictures in your document. When the page loads for the first time, the $(document).ready() function assigns mouse events to the pictures you want to zoom. You just need to change the jQuery selectors in the $(document).ready() function to select the images to zoom if you have a different CSS/HTML layout on your page.

      The showZoom() function is triggered when the user moves the mouse over the image. showZoom() creates the zoom box with the image of actual resolution inside it and calculates the margin ratios required for panning the zoomed image. The moveZoom() function is then used the keep track of the mouse position on the actual image and pan the image in zoom box based upon the mouse position on the actual image (Magnifying Glass Effect). hideZoom() removes the zoom box and its child elements when the user moves the mouse out of the image thumbnail.

JavaScript Code:
<script type="text/javascript"  src=""></script> 
<script type="text/javascript">
/* Developed by: Abhinay Rathore [] */
      $(".pic_zoom > img").mouseover(function(){ showZoom(this); });
      $(".pic_zoom > img").mousemove(function(event){ moveZoom(this, event); });
      $(".pic_zoom > img").mouseout(function(){ hideZoom(); });
var zoom_width = 500; //Change zoom area width here
var zoom_left_ratio, zoom_top_ratio;
function showZoom(picId){ //Show Zoom window 

      //Create and add zoom div (with image) to the body
      var zoom = document.createElement("div"); = "zoom";
      var img = document.createElement("img");
      img.src = picId.src;
      //Set zoom div height proportionally to the width specified above.
      var offset = $(picId).offset();
      $(zoom).height(Math.round($(zoom).width() * $(picId).height() / $(picId).width()));
      $(zoom).css({'top':, 'left': offset.left + $(picId).width() + 10});
      //Calculate zoom ratio
      zoom_left_ratio = ($(img).width() - $(zoom).width()) / $(picId).width();
      zoom_top_ratio = ($(img).height() - $(zoom).height()) / $(picId).height();
function moveZoom(picId, e){ //Move Zoom image
    var zoomed_img = $("#zoom > img");
    var zoom = $("#zoom");
    var pic_offset = $(picId).offset();
    //Get mouse position on the image
    var picX = e.pageX - pic_offset.left;
    var picY = e.pageY -;
    //Calculate margins for the zoomed image
    var leftMargin = -1 * Math.round(picX * zoom_left_ratio);
    var topMargin = -1 * Math.round(picY * zoom_top_ratio);
    //Ser margins for the zoomed image
    zoomed_img.css('left', (leftMargin > 0) ? 0 : leftMargin);
    zoomed_img.css('top', (topMargin > 0) ? 0 : topMargin);
function hideZoom(){ //Hide Zoom window
      //Remove the zoom div and its children from the body

A very minimal amount of CSS code is used to define the div tag holding the images and the properties for the zoom box. If you use your personalized CSS, then there is no need to modify the zoom box properties.

CSS Code:
<style type="text/css">
.pic_zoom {
      border: 1px solid #9CF;
      width: 210px;
.pic_zoom img{
      width: 200px;
      height: auto;
      margin: 5px;
      cursor: crosshair;
#zoom {
      position: absolute;
      border: 1px solid #9CF;
      overflow: hidden;
#zoom img{
      position: absolute;

The HTML code can be modified very easily based upon the layout and style of your HTML document. you can use your own containers or place the images directly on the document. For any modifications in the CSS/HTML layout, the jQuery selectors in the $(document).ready() function needs to be modified to assign the mouse events to the images for zooming them.

HTML Code:
<div class="pic_zoom">
    <img src="WaterLilies.jpg" />
    <img src="goldengate.jpg" />
    <img src="city.jpg" />


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai

  2. mytectra placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance.

  3. Guaranteed #1 Search Engine Ranking Supreme Free Viral Traffic Join Now Get Millions Of Hits Free To Your Site/Blog!

    PornKings Adult Shopping Backlinks-Shopping Mega Store Legendary Stars As Stormy Daniels,Shawna Edwards,Jenna Jamison-New Adult Stars Movies,Adult Toys,Enhancers,Merchandise-More !

    Hits Express Rotator System Do You Need Visitors to Your Website or Affiliate Program? If your looking to gain more visitors to your website Hits Express is your answer. With our program your site is being shown to people all over the world 24/7 365 days a year!

    PAYDIR Free For All Forum Portal Search Engine Crawled Network PR10 Ranking Information Forum XXX!

    FreeLinkExchanges Be Seen In 12 Nations 312 Sites Over 30 Millions Viewers Monthly Buy Featured Link Now With 150 Search Engines Crawling The Network!

    How do I get guaranteed traffic? When someone signs up from your site, they must first click on your classified ad which will open a new window leading to your main website. They will have to wait a few seconds for the code to appear on a separate frame at the top of the screen reach millions free now!

    Blast Your Ad to Over 23,000 Opt-in Prospects at ShowMyLinks Submit Your Solo Email Ad to All Showmylinks Members GET YOUR TEXT LINK ADs LISTED 100% FREE FOR LIFE PLUS EARN MONEY TO YOUR PAYPAL!!

    Adult Store Empires Backlinks Resources Search Engine XXX!

    GET YOUR OWN MONEY-MAKING AD BOARD -- Integrating Text ad, Banner Ad and Email Ad into one Portal Make Money Fast With Your Paypal Reach Million Dollars In A Year Fast !

    Full Length Homemade Videos Watch real people's private home sex videos. Forget about those shitty 1-2 minute clips, these are all high quality full length videos! 40+ Bonus Sites You will also get full access to the entire reelpass network of sites which features over 40+ awesome movie sites, 1000's of movies and pictures to download! Download, Burn & Share Not only do you get to watch all our movies, you can also download every single one and save it forever, burn them to dvd or share with friends!!


Thanks a lot for your valuable comments :)