DHTML Zone



  DHTML


Discovering DHTML in Netscape 6, Part II of II (cont.)

  Clipping
Clipping a layer was a real headache with the older browsers (not that it's any less of a headache now). Netscape 4.x kept each of the clip values within its own property, and allowed access via clip.top and clip.right and the like. In NS6 and IE5, on the other hand, you set clip values using a single string within a style property named, aptly enough, clip. Normally, the clip property is empty unless you assign it either with a stylesheet or the STYLE attribute of the <DIV> tag.

A proper clip value begins with "rect" (to specify the clip as rectangular, as opposed to some other shape that may be supported in the future), followed by the top, right, bottom, and left offset values, in pixels, in that order. Once assembled you can assign the string directly to the <DIV>'s clip property:

var myElement = document.getElementById("sampleDiv");
myElement.style.clip = "rect(0px 50px 50px 20px)";
			

Manipulating an existing clip can be a little tricky. To increment or decrement the clip values, you need to once again extract the individual values and convert them into integers. To get rid of the "rect" and parenthesis, I use a little string magic, splitting the string into an array and then using parseInt to retrieve the integer values and return them in an array.

myElement = document.getElementById("sampleDiv");
   // assumes you've already set a clip value
   var clip = myElement.style.clip 
   var clipVals = clip.split("rect(")[1].split(" ");
   for (var i=0;i<clipVals.length;i++){
     clipVals[i] = parseInt(clipVals[i]);
   }
return clipVals;
			

Now you're free to modify these values, and reassign them to the layer. The three functions below take a layer and re-clip it inward by ten pixels each time:

function clipInward(layerID){
   var myElement = document.getElementById(layerID);
   var clipVals = getClipValues(myElement);
   clipVals[0] += 10;
   clipVals[1] -= 10;
   clipVals[2] -= 10;
   clipVals[3] += 10;
   var newClip = createNewClip(clipVals);
   myElement.style.clip = newClip;
}
function getClipValues(element){
   // assumes you've already set a clip value
   var clip = element.style.clip 
   var clipVals = clip.split("rect(")[1].split(" ");
   for (var i=0;i<clipVals.length;i++){
     clipVals[i] = parseInt(clipVals[i]);
   }
   return clipVals;
}
function createNewClip(clipVals){
   var newClip = "rect(";
   for (var i=0;i<clipVals.length;i++){
     newClip += clipVals[i] + "px ";
   }
   newClip += ")";
   return newClip;
}
			

Example: clipping a layer


1Sizing 2Clipping 3Other Layer Tricks


Back

Next:
 



Resources
W3C DOM Specification

W3C CSS Specification

Netscape 6 Transition Information

Web Standards Project
DevX Links
Get Help with DHTML

web.dhtml.general




Sponsored Links


Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers