Javascript Magnifier Demo

Inspired by this page, but there's pretty much no original code left. Many thanks to Gerard Talbot and 'dhtml' on the comp.lang.javascript newsgroup for many pointers, critiques, and ideas.

Tested on Firefox 2 and 3, Internet Explorer 7, Opera 9.5, and Safari 3.1 for Windows and MacOS X.


Roll the mouse button over the images below to see the magnifiers in action.

example image
treasure map
Vincent Mes Vincent Mes Wikipedia buried treasure entry Wikipedia pirate entry

To use this library for your own purposes

Here's the code for the two examples above:

<div class="magnifier" style="height: 297px; width: 512px; margin: 20px;">
  <div class="maglens">
   <img src="vlag.jpg" class="maglarge" alt="example image" ismap="ismap" usemap="#kaart"
        style="height: 560px; width: 964px;" />
  </div>
</div>

<div class="magnifier" style="height: 249px; width: 253px; margin: 20px">
  <div class="maglens" style="width:100px; height:75px">
    <img src="treasure-map.jpg" class="maglarge" alt="treasure map" ismap="ismap" usemap="#pirate"
      style="height: 374px; width: 380px;" />
  </div>
</div>

You can magnify as many images as you want, as long as they are in different magnifier divs.

If you don't have access to the head section of your HTML, you can't load the CSS file. In that case, use this approach to making magnifiers.

Problems? Check these debugging tips.

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 maglens DIV, like this. The size of the small image will be adjusted to that of the div.

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

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

optical view
x-ray view

Here's the code for the Chandra example:

<div class="magnifier" style="width: 288px; height: 269px">
  <img class="magsmall" src="0157_optical.jpg" alt="optical view" />
  <div class="maglens" style="width:75px; height:75px">
    <img class="maglarge" src="0157_xray.jpg" alt="x-ray view"
     style="width: 288px; height: 269px" />
  </div>
</div>

Changing Default Magnifier Size

To change the default size of the magnifying glass, modify the width and height defined for maglens in magnifier.css:

.maglens {
  position: absolute;
  overflow: hidden;
  width: 150px;
  height: 150px;
}

Image Maps

If you want clickable regions in the image, use an HTML image map. Put the ismap and usemap attributes on the large image. Both examples at the top of this page are image maps.

Working without Classes

If you can't use the CSS classes, you can define magnifiers by explicitly putting the necessary CSS styles on the div elements, as follows:

Here's the code for the previous two examples:

<div style="position:relative; overflow: hidden; height: 249px; width: 253px; margin: 20px">
  <div id="lens1" style="position:absolute; overflow: hidden; width: 100px; height: 75px">
    <img src="treasure-map.jpg" id="large1" alt="treasure map" ismap="ismap" usemap="#pirate"
         style="position:absolute; border-style:none; height: 374px; width: 380px" />
  </div>
</div>

<div style="position: relative; width: 288px; height: 269px">
  <img id="small2" src="0157_optical.jpg" alt="optical view"
       style="position:absolute" />
  <div id="lens2" style="position:absolute; overflow: hidden; width: 75px; height: 75px">
    <img id="large2" src="0157_xray.jpg" alt="x-ray view"
         style="position:absolute; width: 288px; height: 269px" />
  </div>
</div>
treasure map
optical view
x-ray view

Clipping Rectangles

For backward compatibility with previous versions of this package, you can also use clipping rectangles to define the magnifier lens instead of the maglens div. Because browser support for clipping rectangles is poor, this approach is no longer recommended.

Note: Avoid commas in the rectangle if you need to support Internet Explorer.

Here's the code for the two examples, using clipping rectangles. Note that there is no maglens div:

<div style="position:relative; overflow: hidden; height: 249px; width: 253px; margin: 20px">
 <img src="treasure-map.jpg" id="large3" alt="treasure map" ismap="ismap" usemap="#pirate"
    style="position:absolute; border-style:none; height: 374px; width: 380px; clip:rect(0px 100px 75px 0px);" />
</div>

<div style="position: relative; width: 288px; height: 269px">
  <img id="small4" src="0157_optical.jpg" alt="optical view"
     style="position:absolute" />
  <img id="large4" src="0157_xray.jpg" alt="x-ray view"
     style="position:absolute; width: 288px; height: 269px; clip: rect(0px 75px 75px 0px);" />
</div>
treasure map
optical view x-ray view

Troubleshooting

The magnifier tries to catch many common errors with images. If no magnifier appears when you roll over your images, or the magnifier is distorted or off-center, check the following:

How it works

As the mouse moves over the small image, the Javascript repositions the lens (either the maglens div or the clipping rectanthe mouse, and positionsgle) to stay centered under the large image so that what's the lens shows is the region that corresponds to where the mouse is in the small image.


Send comments to Chris.Riesbeck@gmail.com


Valid XHTML 1.0 Transitional Valid CSS! CSS valid except for commas in clip, required by Internet Explorer