04.11.2015 Views

javascript

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

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

Chapter 11: DOM Levels 2 and 3<br />

(continued)<br />

}<br />

document.body.appendChild(temp);<br />

arguments.callee.offset = -temp.getBoundingClientRect().top -<br />

scrollTop;<br />

document.body.removeChild(temp);<br />

temp = null;<br />

var rect = element.getBoundingClientRect();<br />

var offset = arguments.callee.offset;<br />

return {<br />

left: rect.left + offset,<br />

right: rect.right + offset,<br />

top: rect.top + offset,<br />

bottom: rect.bottom + offset<br />

};<br />

} else {<br />

var actualLeft = getElementLeft(element);<br />

var actualTop = getElementTop(element);<br />

}<br />

}<br />

return {<br />

left: actualLeft - scrollLeft,<br />

right: actualLeft + element.offsetWidth - scrollLeft,<br />

top: actualTop - scrollTop,<br />

bottom: actualTop + element.offsetHeight - scrollTop<br />

}<br />

This function uses the native getBoundingClientRect() method when it ’ s available and defaults to<br />

calculating the dimensions when it is not. There are some instances where the values will vary in<br />

browsers, such as with layouts that use tables or scrolling elements.<br />

Prior to Firefox 3, a method called getBoxObjectFor() was available. This method<br />

originated in XUL and leaked into the web browser due to its location in the class<br />

hierarchy. It is recommended that you avoid using this method in web development.<br />

Traversals<br />

The DOM Level 2 Traversal and Range module defines two types that aid in sequential traversal of a<br />

DOM structure. These types, NodeIterator and TreeWalker , perform depth - first traversals of a DOM<br />

structure given a certain starting point. These object types are available in DOM - compliant browsers,<br />

including Firefox 1 and later, Safari 1.3 and later, Opera 7.6 and later, and Chrome 0.2 and later. There is<br />

no support for DOM traversals in IE. You can test for DOM Level 2 Traversal support using the<br />

following code:<br />

342

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

Saved successfully!

Ooh no, something went wrong!