02.06.2013 Views

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

JavaScript & jQuery: The Missing Manual ... - Robert Guajardo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Determining the Size<br />

and Position of Page<br />

Elements<br />

1<br />

document<br />

2<br />

<br />

3<br />

position : absolute;<br />

top: 300px;<br />

left: 100px<br />

<br />

position : absolute;<br />

top: 250px;<br />

left: 200px<br />

324 javascript & jquery: the missing manual<br />

Figure 10-7:<br />

<strong>jQuery</strong> provides two functions that let you determine the<br />

top and left position of an element on the page. In the<br />

case where you have an absolutely positioned element<br />

(#innerBox) inside another positioned element (#outer-<br />

Box), the two functions will return different results.<br />

Tip: You can also use the offset() function to set the position of an element on a page; just pass the function<br />

an object with left and top values like this:<br />

$('#element').offset({<br />

left : 100,<br />

top : 200<br />

});<br />

You can only use pixel values for left and top; em values—(20em)—or percentages—(20%)—won’t work.<br />

Determining a Page’s Scrolling Position<br />

A web page is often larger than the browser window that displays that page: Web<br />

documents with lots of content are frequently taller and sometimes wider than the<br />

browser, forcing visitors to scroll to see all of the page (see Figure 10-8). When a<br />

visitor scrolls a page, some of the document disappears from view. For example,<br />

In Figure 10-8, the web page doesn’t fit in the browser window, the document is<br />

scrolled in left and up, so the top of the page and left side of the page are out of view.<br />

This means the top-left corner of the browser window isn’t the same as the top-left<br />

corner of the document. If you tried to place a new element like an animated banner<br />

at the top of the screen, you’d run into trouble if you merely set the left and top position<br />

of the element to 0—you’d actually be placing it at the top of the document, but<br />

outside of the browser window.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!