Execute a JavaScript when a <div> element is being scrolled: - Javascript DOM Event

Javascript examples for DOM Event:onscroll

Description

Execute a JavaScript when a <div> element is being scrolled:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/*  www  . j  a  v a  2  s . co  m*/
    border: 1px solid black;
    width: 200px;
    height: 100px;
    overflow: scroll;
}
</style>
</head>
<body>

<p>Try the scrollbar in div.</p>

<div onscroll="myFunction()">
This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
<br><br>
This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
<br><br>
This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
<br><br>

</div>

<p>Scrolled <span id="demo">0</span> times.</p>

<script>
var x = 0;
function myFunction() {
    document.getElementById("demo").innerHTML = x += 1;
}
</script>

</body>
</html>

Related Tutorials