04.11.2015 Views

javascript

Create successful ePaper yourself

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

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

implementations are slightly different. IE considers the upper - left corner of the document to be located<br />

at (2,2), whereas the Firefox and Opera implementations use the traditional (0,0) as the starting<br />

coordinates. This necessitates doing an initial check for the location of an element positioned at (0,0),<br />

which will return (2,2) in IE and (0,0) in other browsers. Here is an example:<br />

function getBoundingClientRect(element){<br />

if (typeof arguments.callee.offset != “number”){<br />

var scrollTop = document.documentElement.scrollTop;<br />

var temp = document.createElement(“div”);<br />

temp.style.cssText = “position:absolute;left:0;top:0;”;<br />

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

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

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

temp = null;<br />

}<br />

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

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

}<br />

return {<br />

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

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

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

bottom: rect.bottom + offset<br />

};<br />

This function uses a property on itself to determine the necessary adjustment for the coordinates. The<br />

first step is to see if the property is defined and if not, define it. The offset is defined as the negative<br />

value of a new element ’ s top coordinate, essentially setting it to – 2 in IE and – 0 in Firefox and Opera. To<br />

figure this out, it requires creating a temporary element, setting its position to (0,0), and then calling<br />

getBoundingClientRect() . The scrollTop of the viewport is subtracted from this value just in case<br />

the window has already been scrolled when the method is called. Using this construct ensures that you<br />

don ’ t have to call getBoundingClientRect() twice each time this function is called. Then, the method<br />

is called on the element and an object is created with the new calculations.<br />

For browsers that don ’ t support getBoundingClientRect() , the same information can be gained by<br />

using other means. Generally, the difference between the right and left properties is equivalent to<br />

offsetWidth , and the difference between the bottom and top properties is equivalent to<br />

offsetHeight . Further, the left and top properties are roughly equivalent to using the<br />

getElementLeft() and getElementTop() functions defined earlier in this chapter. A cross - browser<br />

implementation of the function can be created as shown in the following example:<br />

function getBoundingClientRect(element){<br />

var scrollTop = document.documentElement.scrollTop;<br />

var scrollLeft = document.documentElement.scrollLeft;<br />

if (element.getBoundingClientRect){<br />

if (typeof arguments.callee.offset != “number”){<br />

var temp = document.createElement(“div”);<br />

temp.style.cssText = “position:absolute;left:0;top:0;”;<br />

(continued)<br />

341

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

Saved successfully!

Ooh no, something went wrong!