How to identify which element scroll is being used using JavaScript ?
To identify which element is being scrolled using JavaScript, you can attach scroll event listeners to specific elements. This allows you to determine which element’s scroll event is active, enabling tailored responses or interactions based on the particular element being scrolled.
Using Scroll event listener
This approach involves adding scroll event listeners to two scrollable elements. When either element is scrolled, the event listener updates the text content of a designated result area to indicate which element is being scrolled. This allows for real-time feedback on user interactions with the scrollable elements.
Example: In this example, we’ve created two scrollable elements and added the ‘scroll’ event listener to both of them. As soon as any of the elements are scrolled, the scroll event of that particular element is fired.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport"
content="width=device-width,
initial-scale=1.0" />
<title>How to identify which element
scroll is being used using JavaScript?
</title>
<style>
h1 {
color: #2f8d46;
font-weight: bold;
margin: 40px;
}
#first {
height: 200px;
width: 400px;
overflow-y: scroll;
margin: 40px;
}
#second {
margin: 40px;
height: 200px;
width: 400px;
overflow-y: scroll;
}
#scrolled {
margin: 40px;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<!--First element-->
<div id="first">
<h2>First element</h2>
<p>
JavaScript is a lightweight, cross-platform
and interpreted scripting language. It is well-known
for the development of web pages, many non-browser
environments also use it. JavaScript can be used for
Client-side developments as well as Server-side
developments. JavaScript contains a standard library
of objects, like Array, Date, and Math, and a core
set of language elements like operators, control
structures, and statements.
History of JavaScript: It was created in 1995
by Brendan Eich while he was an engineer at Netscape.
It was originally going to be named LiveScript but
was renamed. Unlike most programming languages,
the JavaScript language has no concept of input
or output. It is designed to run as a scripting
language in a host environment, and it is up to
the host environment to provide mechanisms for
communicating with the outside world. The most
common host environment is the browser.
</p>
</div>
<!--Second element-->
<div id="second">
<h2>Second element</h2>
<p>
JavaScript is a lightweight, cross-platform
and interpreted scripting language. It is well-known
for the development of web pages, many non-browser
environments also use it. JavaScript can be used for
Client-side developments as well as Server-side
developments. JavaScript contains a standard library
of objects, like Array, Date, and Math, and a core
set of language elements like operators, control
structures, and statements.
History of JavaScript: It was created in 1995
by Brendan Eich while he was an engineer at Netscape.
It was originally going to be named LiveScript but
was renamed. Unlike most programming languages,
the JavaScript language has no concept of input
or output. It is designed to run as a scripting
language in a host environment, and it is up to
the host environment to provide mechanisms for
communicating with the outside world. The most
common host environment is the browser.
</p>
</div>
<div id="scrolled">
<h3>Element being scrolled: <span id="result"></span></h3>
</div>
<script>
//Selecting required elements from the DOM
const first = document
.querySelector("#first");
const second = document
.querySelector("#second");
const result = document
.querySelector("#result");
//Adding the scroll event listener
first
.addEventListener("scroll",
() => (result.textContent = "First"));
second
.addEventListener("scroll",
() => (result.textContent = "Second"));
</script>
</body>
</html>
Output:
You can see the output of the project below: