Javascript DOM onscroll Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("scroll",
       myScript);

This example uses the addEventListener() method to attach a "scroll" event to a div element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/*from   w w  w.j  ava 2s .  c  om*/
  border: 1px solid black;
  width: 200px;
  height: 100px;
  overflow: scroll;
}
</style>
</head>
<body>

<p>Try the scrollbar in the div</p>
<div id="myDIV">
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
This is a test. <br><br>
</div>

<p id="demo"></p>

<script>
document.getElementById("myDIV").addEventListener("scroll", myFunction);

function myFunction() {
  document.getElementById("demo").innerHTML = "You scrolled in div.";
}
</script>

</body>
</html>
Bubbles:
Cancelable:
Event type:
Supported HTML tags:

























No
No
UiEvent if generated from a user interface, Event otherwise
<address>,
<blockquote>,
<body>,
<caption>,
<center>,
<dd>,
<dir>,
<div>,
<dl>,
<dt>,
<fieldset>,
<form>,
<h1> to <h6>,
<html>,
<li>,
<menu>,
<object>,
<ol>,
<p>,
<pre>,
<select>,
<tbody>,
<textarea>,
<tfoot>,
<thead>,
<ul>



PreviousNext

Related