HTML onscroll Attribute

HTML onscroll Attribute: The HTML onscroll attribute is defined as it fires when an element’s scrollbar is being scrolled. The onscroll attribute is the part of the event attribute. HTML4 does not support this attribute. It was supported by HTML5.

HTML onscroll Attribute

This HTML onscroll attribute can be applied to all the HTML elements.

Syntax:<div onscroll=”myFunction()”>

Browser Support

The HTML onscroll attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari
  • Opera

Example: for <div> element

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
  border: 1px solid black;
  width: 200px;
  height: 100px;
  overflow: scroll;
}
</style>
</head>
<body>
<p>Try the scrollbar in div.</p>

<div id="myDIV" onscroll="myFunction()">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
<br><br>
</div>

<script>
function myFunction() {
  document.getElementById("myDIV").style.color = "red";
}
</script>
</body>
</html>

Output:

HTML onscroll attribute