15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Example<br />

The following IE script statements utilize all four “offset” dimensional properties<br />

to size and position a DIV element so that it completely covers a SPAN element<br />

located within a P element. This can be for a fill-in-the-blank quiz that provides text<br />

entry fields elsewhere on the page. As the user gets an answer correct, the blocking<br />

DIV element is hidden to reveal the correct answer.<br />

document.all.blocker.style.pixelLeft = document.all.span2.offsetLeft<br />

document.all.blocker.style.pixelTop = document.all.span2.offsetTop<br />

document.all.blockImg.height = document.all.span2.offsetHeight<br />

document.all.blockImg.width = document.all.span2.offsetWidth<br />

Because the offsetParent property for the SPAN element is the BODY element,<br />

the positioned DIV element can use the same positioning context (it’s the default<br />

context, anyway) for setting the pixelLeft and pixelTop style properties.<br />

(Remember that positioning properties belong to an element’s style object.) The<br />

offsetHeight and offsetWidth properties can read the dimensions of the SPAN<br />

element (the example has no borders, margins, or padding to worry about) and<br />

assign them to the dimensions of the image contained by the blocker DIV element.<br />

This example is also a bit hazardous in some implementations. If the text of span2<br />

wraps to a new line, the new offsetHeight value has enough pixels to accommodate<br />

both lines. But the blockImg and blocker DIV elements are block-level elements that<br />

render as a simple rectangle. In other words, the blocker element doesn’t turn into<br />

two separate strips to cover the pieces of span2 that spread across two lines.<br />

offsetParent<br />

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

Compatibility ✓ ✓ ✓ ✓<br />

Example<br />

You can use the offsetParent property to help you locate the position of a<br />

nested element on the page. Listing 15-12 demonstrates how a script can “walk” up<br />

the hierarchy of offsetParent objects in IE for Windows to assemble the location of<br />

a nested element on a page. The goal of the exercise in Listing 15-12 is to position an<br />

image at the upper-left corner of the second table cell. The entire table is centered<br />

on the page.<br />

The onLoad event handler invokes the setImagePosition() function. The<br />

function first sets a Boolean flag that determines whether the calculations should<br />

be based on the client or offset sets of properties. IE4/Windows and IE5/Mac rely<br />

on client properties, while IE5+/Windows works with the offset properties. The<br />

discrepancies even out, however, with the while loop. This loop traverses the<br />

offsetParent hierarchy starting with the offsetParent of the cell out to, but not<br />

including, the document.body object. The body object is not included because that<br />

is the positioning context for the image. In IE5, the while loop executes only once<br />

33<br />

elementObject.offsetParent

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

Saved successfully!

Ooh no, something went wrong!