How to identify which element scroll is being used using JavaScript ?
Given many elements inside the DOM, the task is to find out which element is being currently scrolled using JavaScript.
Approach: This problem can be easily solved using JavaScript. We will add the ‘scroll’ event listener to all the required elements. The scroll event is fired whenever a particular element is being scrolled. Thus, we can easily find out which element is being scrolled with the help of it.
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 element is scrolled, the scroll event of that particular element is fired.
HTML
<!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 >w3wiki</ 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: