Javascript DOM onscroll Event via HTML Tag onscroll function

Introduction

In JavaScript:

object.onscroll = function(){
       myScript};

This example uses the HTML DOM to assign an "onscroll" event to a div element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {/* w  w  w. jav a  2  s  . c o  m*/
  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").onscroll = function() {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