spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / dhtml / column2
Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

Logo

Cutting Your Element Down To Size
2 minutes for clipping


Clipping before Initial Display

CSS allows us to set the visible part of our element, before it is initially displayed, using the clip property:

The only shape presently supported is a rectangle. If we wanted to only show a 100x120 pixel portion of our element 20 pixels in from the left and 40 from the top, our declaration would be:

Since, however, Image2 is initially hidden, we do not set the initial clipping. Even if we did, we would change it through scripting before display. We have included the above snippet FYI only.

Dynamic Clipping

In Navigator 4

Clip properties are reflected into Netscape JavaScript as:

To clip our image to show only the part corresponding to the second link, for example, we need only:

We do not need to reference or change clip.top or clip.bottom because they remain constant.

In Explorer 4

Explorer stores an object's clip property value in a single string, retaining the CSS format. For example, if our object's CSS clip declaration was:

the property document.all.myObject.style.clip would store the string:

Explorer adds the pixel identifier internally. We do not need to include it if we change the clip value. But, unlike Navigator, Explorer expects all four clip values.

Our second link clip would need this code in Explorer:

Try it, by placing your mouse anywhere on the menu:

Build the Clipping Script

To make life easier, we need to store the clipping values for the 7 links in a two-dimensional array so that our mapOver function can reference them:

Declare the array:

Write a function that creates a second array off each array element and populates it as necessary. In this way, our code becomes modular and can be used in other cases with more or fewer elements.
The function accepts three arguments:
which: the link in our image map (an integer from 1 to 7);
from: the left clip value, and
to: the right clip value

Call the function, taking the clip values from the <AREA> tags:

To make our Explorer scripting more readable later, we store the constant top and bottom clip values in two variables:

In redefining our mapOver function, we must add an additional argument identifying which link should be highlighted (clipped). We will use this argument to reference our array. To minimize the nested ifs in the function, we now check immediately for a mouseout, hide the element and return. If it is a mouseover, we proceed by initializing variables for the left and right clip values, and then check for browser and execute the appropriate script.

And modify your map for the last time, adding values for the new first argument. eg. mapOver(1,true).

The Final Product

Once again, let's take all the code snippets that we have introduced, tighten them up, and see the complete code for our effect:


Produced by Peter Belesis and

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji

All Rights Reserved. Legal Notices.
Created: 08/06/97
Revised: 10/16/97

URL: http://www.webreference.com/dhtml/column2/clipping.html