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 10: The Document Object Model<br />

Scrolling<br />

One of the issues not addressed by the DOM specification is how to scroll areas of a page. To fill this gap,<br />

browsers have implemented several methods that control scrolling in different ways. Each of the<br />

following methods exists as an extension to the HTMLElement type and therefore each is available on all<br />

elements:<br />

❑<br />

❑<br />

❑<br />

❑<br />

scrollIntoView( alignWithTop ) — Scrolls the browser window or container element so the<br />

element is visible in the viewport. If alignWithTop is set to true or is omitted, the window<br />

scrolls so that the top of the element is at the top of the viewport (if possible). This is implemented<br />

in all browsers.<br />

scrollIntoViewIfNeeded( alignCenter ) — Scrolls the browser window or container<br />

element so that the element is visible in the viewport only if it ’ s not already visible; if<br />

the element is already visible in the viewport, this method does nothing. The optional<br />

alignCenter argument will attempt to place the element in the center of the viewport if set to<br />

true . This is implemented in Safari and Chrome.<br />

scrollByLines( lineCount ) — Scrolls the contents of the element by the height of the given<br />

number of text lines, which may be positive or negative. This is implemented in Safari and Chrome.<br />

scrollByPages( pageCount ) — Scrolls the contents of the element by the height of a page,<br />

which is determined by the height of the element. This is implemented in Safari and Chrome.<br />

Keep in mind that scrollIntoView() and scrollIntoViewIfNeeded() act on the element ’ s<br />

container, whereas scrollByLines() and scrollByPages() affect the element itself. Following is an<br />

example of how this may be used:<br />

//scroll body by five lines<br />

document.body.scrollByLines(5);<br />

//make sure this element is visible<br />

document.forms[0].scrollIntoView();<br />

//make sure this element is visible only if it’s not already<br />

document.images[0].scrollIntoViewIfNeeded();<br />

//scroll the body back up one page<br />

document.body.scrollByPages(-1);<br />

Because scrollIntoView() is the only method supported in all browsers, this is typically the only<br />

one used.<br />

The children Property<br />

The differences in how IE and other browsers interpret white space in text nodes led to the creation of<br />

the children property. The children property is an HTMLCollection that contains only an element ’ s<br />

child nodes that are also elements. Otherwise the children property is the same as childNodes and<br />

may contain the same items when an element has only elements as children. The children property is<br />

accessed as follows:<br />

298

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

Saved successfully!

Ooh no, something went wrong!