Use window.onscroll to add the onscroll event handler in JavaScript

Description

The following code shows how to use window.onscroll to add the onscroll event handler.

Example


<html>
<head>
<script type="text/javascript">
window.onscroll = function () {<!--   ww  w .  j av  a 2 s.c o m-->
document.writeln("scrolling");
}
</script>
</head>
<body>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
<P>Try scrolling this window.</p>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use window.onscroll to add the onscroll event handler in JavaScript