Javascript Magnifier Demo

There are two versions of this library. This one uses clipping rectangles. The other version uses clipping div elements. Browser support for clipping rectangles is shakey and the Javascript code is a bit messy so I prefer the other version myself. Both versions have been tested on Firefox 2, Internet Explorer 7, Opera 9.5, and Safari 3.1 for Windows and MacOS X.

Original code from here. It's been pretty much completely rewritten to use current approved methods for capturing mouse events, support multiple images, anywhere on the page, with non-square individually-sized magnifiers.

Move the mouse over the images below to see the magnifiers in action. Note that the right magnifier is smaller than the left magnifier.

Vincent Mes Vincent Mes Wikipedia buried treasure entry Wikipedia pirate entry

To use this library for your own purposes

You can magnify as many images as you want, as long as they are in different magnifier div's, with the required class magnifier on the div and maglarge on the IMG.

Problems? Check here.

Options

Custom Small Image

By default, the Javascript creates the reduced image to display. If you want to use a customized small image instead, put the IMG element for it in the div, with the class magsmall, ahead of the large image, like this. Note that the div and the magsmall image should have the same size.

<div class="magnifier" style="width: swpx; height: shpx;">
  <img src="image-file" class="magsmall"
       style="width: swpx; height: shpx;">
  
  <img src="image-file" class="maglarge"
       style="width: lwpx; height: lhpx;">
</div>

You can also use two images of the same size to do "reveal" rather than magnification. For example, here the "magnifier" shows what a galaxy looks like in the X-ray spectrum. Images from the Chandra X-ray Observatory.

visible light view

Custom Magnifier Size

The default magnifier is 150 x 150 pixels. To get a different size, add a clip:rect(0px, right, bottom, 0px>) to the style of the large image. Put the desired width and height in the right and bottom, respectively, like this:

<div class="magnifier" style="width: swpx; height: shpx;">
  <img src="image-file" class="maglarge"
     style="width: lwpx; height: lhpx; clip:rect(0px mwpx mhpx 0px)">
</div>

where mw and mh are the desired width and height of the magnifier.

To change the size of the default magnifying glass, modify the default clip rectangle defined for maglarge in magnifier.css:

.maglarge {
  position: absolute; 
  clip: rect(0px 150px 150px 0px); 
  border-style: none
}

Image Maps

If you want clickable regions in the image, use an HTML image map. Put the ISMAP and USEMAP attributes on the IMG for the large image. Both images on this page are image maps.

Working without Classes

If for some reason, you can't use CSS classes, you can define magnifiers by using names, ids, and inline styles, like this:

<div id="magnifier" style="position:relative; overflow: hidden; width: swpx; height: shpx">
 <IMG src="image-file" name="large"
    style="position:absolute; width: lwpx; height: lhpx" />
</div>

The following needs to be specified:

Troubleshooting

If no magnifier appears or the magnifier is distorted or off-center, check the following:

How it works

When the mouse is over the small image, the Javascript code creates a small view, called a clip rectangle in CSS-speak, of the same region of the large hidden image. As the mouse moves over the small image, the Javascript positions the clip region over the corresponding point in the large image, and moves the large image so that the clip region remains centered under the mouse.