18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

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 9<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

<br />

<br />

You can also use this event in conjunction with several properties of the element, namely<br />

scrollLeft, which tells you how far the window has scrolled horizontally, and scrollTop, which tells<br />

you how far the window has scrolled vertically:<br />

<br />

<br />

OnScroll Example<br />

<br />

window.onscroll = function () {<br />

var oTextbox = document.getElementById(“txt1”);<br />

oTextbox.value += “\nscroll is at “ + document.body.scrollLeft + “<br />

horizontally and “ + document.body.scrollTop + “ vertically.”;<br />

}<br />

<br />

<br />

<br />

Try scrolling this window.<br />

<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

&nbsp;<br />

<br />

<br />

In this example, a text box is used to track the scrollLeft and scrollTop properties so you can see<br />

the changes accurately. This works on all major browsers and can be used to create cool effects like a<br />

watermark that always appears at the top of the page:<br />

<br />

<br />

OnScroll Example<br />

290

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

Saved successfully!

Ooh no, something went wrong!