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.

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

Two controls enable you to set the position of an underlying highlight rectangle<br />

to any line of your choice. A checkbox enables you to set whether the highlight<br />

rectangle should be only as wide as the line or the full width of the bounding rectangle<br />

for the entire SPAN element.<br />

All the code is located in the hilite() function. The SELECT and checkbox elements<br />

invoke this function. Early in the function, the getClientRects() method is<br />

invoked for the main element to capture a snapshot of all TextRectangles for the<br />

entire element. This array comes in handy when the script needs to get the coordinates<br />

of a rectangle for a single line, as chosen in the SELECT element.<br />

Whenever the user chooses a number from the SELECT list and the value is less<br />

than the total number of TextRectangle objects in clientRects, the function<br />

begins calculating the size and location of the underlying yellow highlighter. When<br />

the Full Width checkbox is checked, the left and right coordinates are obtained<br />

from the getBoundingClientRect() method because the entire SPAN element’s<br />

rectangle is the space you’re interested in; otherwise, you pull the left and right<br />

properties from the chosen rectangle in the clientRects array.<br />

Next comes the assignment of location and dimension values to the hiliter<br />

object’s style property. The top and bottom are always pegged to whatever line is<br />

selected, so the clientRects array is polled for the chosen entry’s top and bottom<br />

properties. The previously calculated left value is assigned to the hiliter object’s<br />

pixelLeft property, while the width is calculated by subtracting the left from the<br />

right coordinates. Notice that the top and left coordinates also take into account<br />

any vertical or horizontal scrolling of the entire body of the document. If you resize<br />

the window to a smaller size, line wrapping throws off the original line count.<br />

However, an invocation of hilite() from the onResize event handler applies the<br />

currently chosen line number to whatever content falls in that line after resizing.<br />

Listing 15-27: Using getBoundingClientRect()<br />

<br />

<br />

getClientRects() and getBoundClientRect() Methods<br />

<br />

function hilite() {<br />

var hTop, hLeft, hRight, hBottom, hWidth<br />

var select = document.forms[0].choice<br />

var n = parseInt(select.options[select.selectedIndex].value) - 1<br />

var clientRects = document.all.main.getClientRects()<br />

var mainElem = document.all.main<br />

if (n >= 0 && n < clientRects.length) {<br />

if (document.forms[0].fullWidth.checked) {<br />

hLeft = mainElem.getBoundingClientRect().left<br />

hRight = mainElem.getBoundingClientRect().right<br />

} else {<br />

hLeft = clientRects[n].left<br />

hRight = clientRects[n].right<br />

}<br />

document.all.hiliter.style.pixelTop = clientRects[n].top +<br />

Continued<br />

71<br />

elementObject.getBoundingClientRect()

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

Saved successfully!

Ooh no, something went wrong!