15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

clientHeight<br />

clientWidth<br />

Chapter 1 ✦ Generic HTML Element Objects (Chapter 15)<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓<br />

Example<br />

Listing 15-6 calls upon the clientHeight and clientWidth properties of a DIV<br />

element that contains a paragraph element. Only the width of the DIV element is<br />

specified in its style sheet rule, which means that the paragraph’s text wraps inside<br />

that width and extends as deeply as necessary to show the entire paragraph. The<br />

clientHeight property describes that depth. The clientHeight property then<br />

calculates where a logo image should be positioned immediately after DIV, regardless<br />

of the length of the text. As a bonus, the clientWidth property helps the script<br />

center the image horizontally with respect to the paragraph’s text.<br />

Listing 15-6: Using clientHeight and clientWidth Properties<br />

<br />

<br />

clientHeight and clientWidth Properties<br />

<br />

function showLogo() {<br />

var paragraphW = document.all.myDIV.clientWidth<br />

var paragraphH = document.all.myDIV.clientHeight<br />

// correct for Windows/Mac discrepancies<br />

var paragraphTop = (document.all.myDIV.clientTop) ?<br />

document.all.myDIV.clientTop : document.all.myDIV.offsetTop<br />

var logoW = document.all.logo.style.pixelWidth<br />

// center logo horizontally against paragraph<br />

document.all.logo.style.pixelLeft = (paragraphW-logoW)/2<br />

// position image immediately below end of paragraph<br />

document.all.logo.style.pixelTop = paragraphTop + paragraphH<br />

document.all.logo.style.visibility = “visible”<br />

}<br />

<br />

<br />

<br />

Position and Show Logo Art<br />

<br />

<br />

Continued<br />

13<br />

elementObject.clientHeight

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!