Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain Scroll events in JavaScript.
The scroll event in JavaScript is fired when the user interacts with a scrollbar by moving it up or down.
Following is the code for scroll events in JavaScript −
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
height: 150vh; /*To have a scrollbar*/
}
.result{
font-size: 18px;
color: blueviolet;
font-weight: 500;
}
</style>
</head>
<body>
<h1>Scroll events in JavaScript</h1>
<div class="result"></div>
<h3>Scroll the scrollbar to see some text in the above paragraph</h3>
<script>
let resEle = document.querySelector(".result");
window.addEventListener("scroll", () => {
resEle.innerHTML = "The scroll event has fired";
});
</script>
</body>
</html>
Output

On scrolling down a little −

Advertisements