Webmaster

DHTML : Pages with customised scrollbars
Contents ]
Dr Benton

The Art of Clipping

Clipping is a function that is applied to layers. Its role is to display part of a layer only. For instance, if you had a layer containing an image measuring 100 x 100 pixels, you could define a clipping area for the layer so that only the upper left quarter of the image would be displayed in the browser:

<div id="layer_id" style="position:absolute; left:0px; top:0px; width:100px; height:100px;clip:'rect(0px 50px 50px 0px)'">

A strange syntax! The syntax of rect() is somewhat strange. For some reason, the coordinates of the clipping area (visible part) are indicated in the following order:

  1. Vertical coordinate of the upper right corner.

  2. Horizontal coordinate of the upper right corner.

  3. Vertical coordinate of the lower left corner.

  4. Horizontal coordinate of the lower left corner.


But the coordinates of the clipping area are not set permanently. You can change them with just a few lines of JavaScript. The code used depends on the visitor's browser. For instance, to make the lower right quarter of our previous layer example (identified by "layer_id") visible, the JavaScript code should read as follows:

  • For Netscape Navigator, we will directly modify the clip.left, clip.right, clip.top and clip.bottom properties to redefine the clipping area:

document.layers["layer_id"].clip.left = 50;
document.layers["layer_id"].clip.right = 100;
document.layers["layer_id"].clip.top = 50;
document.layers["layer_id"].clip.bottom = 100;

  • For Microsoft Internet Explorer, the clipping area is defined as a rectangle with coordinates given between parentheses. We then pass the rect() to the clip property of the style attribute of the layer:

  • ev = 'rect(50px 100px 100px 50px)';
    document.all["layer_id"].style.clip = ev;



  1   2   3   4   5   6   7