Wednesday, March 24, 2010

JavaScript and jQuery Image Magnifier

In an alternate version to the Image Magnification tool from my previous post, I have created another magnification tool which works exactly like a magnification glass moving over the picture. This tool is more concise and easy to use then the previous one where a separate popup with the same aspect ratio was used to display the image. Here the magnified image section keeps moving with the mouse providing an exact magnification glass effect. You also don’t need any separate CSS for the magnification window.

Why should you use this Image Magnification tool:

  1. Zero coding required.
  2. No CSS required at all.
  3. Can be triggered automatically or manually from the image.
  4. Works on images of any resolution or aspect ratio.

For testing I have used a very high resolution image of Taj Mahal (4300x2724 pixels).

This is how it looks in action:JavaScript Image MagnifierThe JavaScript code is very easy to understand and modify. The only thing you will need to modify for automatic magnification functionality is the $(document).ready() function where the jQuery Selectors needs to be modified to point to the images in your HTML document. You can also change the magnifier window size by changing the magnifier_size variable in the JavaScript Code.

Place the following JavaScript Code in the head section of your HTML page:
<script type="text/javascript"  src=""></script> 
<script type="text/javascript">
/* Developed by: Abhinay Rathore [] */
      $(".magnify").mousemove(function(event){ moveMagnifier(this, event); });
      $(".magnify").mouseout(function(event){ hideMagnifier(); });

var magnifier_size = 200; //Change Magnifier size here.
var cursor_offset = 10; //Change Cursor Offset here.
var ratio, mHalf, pic_offset;
function showMagnifier(picId, e){ //Show Magnifier
      //Create and add magnified image to the body
      var magnifier = document.createElement("img"); = "magnifier"
      magnifier.src = picId.src; = "absolute";
      //Calculate size ratio of original and magnified images
      ratio = magnifier.width / $(picId).width();
      mHalf = magnifier_size / 2;
      pic_offset = $(picId).offset(); //Picture offset
      //Get mouse position on the image
      var picX = e.pageX - pic_offset.left;
      var picY = e.pageY -;
      clipImage(magnifier, picX, picY); //Clip the magnified image
      //Position the magnified image next to mouse cursor
      positionImage(magnifier, picX, picY, e.pageX, e.pageY)
function moveMagnifier(picId, e){ //Move Magnifier
      var magnifier = document.getElementById("magnifier");
      if(magnifier){ //If magnified image exists...
            //Get mouse position on the image
            var picX = e.pageX - pic_offset.left;
            var picY = e.pageY -;
            clipImage(magnifier, picX, picY); //Clip the magnified image
            //Position the magnified image next to mouse cursor
            positionImage(magnifier, picX, picY, e.pageX, e.pageY)
      } else { //If magnified image does not exist...
            showMagnifier(picId, e);
function clipImage(magnifier, picX, picY){ //Clip magnified image...
      var centerX = picX * ratio;
      var centerY = picY * ratio;
      $(magnifier).css("clip", "rect(" + Math.round(centerY - mHalf)  + "px," +
                                                   Math.round(centerX + mHalf) + "px," +
                                                   Math.round(centerY + mHalf) + "px," +
                                                   Math.round(centerX - mHalf) + "px)");
function positionImage(magnifier, picX, picY, pageX, pageY){  //Position magnified image...
      $(magnifier).css({'top': pageY - (picY * ratio) + mHalf + cursor_offset,
                         'left': pageX - (picX * ratio) + mHalf + cursor_offset});
function hideMagnifier(){ //Hide Magnifier
      $("#magnifier").remove(); //Remove the Magnifier Image

The CSS code I have used here is only to display the original images on the HTML page. You can use you own CSS styles and use the image class in the jQuery Selectors. You actually don’t need any CSS code to use this tool. You can use it directly by assigning mouse events to your images manually.
<style type="text/css">
      width: 500px;
      height: auto;
      cursor: crosshair;

The HTML Code for this image magnifier is very simple to use. If you are using CSS layout then you can use jQuery Selectors to select the images automatically.
<img class="magnify" src="taj.jpg" />

If you want to assign Magnification feature to your images manually, you can add mouse events to your images as shows below: 
<img src="taj.jpg" style="width:500px" onmousemove="moveMagnifier(this, event)" onmouseout="hideMagnifier()" />


  1. It is very useful. Good work done.

  2. 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

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

  4. 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 :)