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="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript">
/* Developed by: Abhinay Rathore [web3o.blogspot.com] */
$(document).ready(function(){
      $(".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 
      hideZoom();

      //Create and add zoom div (with image) to the body
      var zoom = document.createElement("div");
      zoom.id = "zoom";
      var img = document.createElement("img");
      img.src = picId.src;
      zoom.appendChild(img);
      document.body.appendChild(zoom);
      //Set zoom div height proportionally to the width specified above.
      var offset = $(picId).offset();
      $(zoom).width(zoom_width);
      $(zoom).height(Math.round($(zoom).width() * $(picId).height() / $(picId).width()));
      $(zoom).css({'top': offset.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 - pic_offset.top;
    //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
      $("#zoom").children().remove();
      $("#zoom").remove();
}
</script>

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;
}
</style>

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" />
</div>

2 comments:

Thanks a lot for your valuable comments :)