Is it possible for an element that is styled with 'overflow: scroll' to cause the

I am currently facing an issue with adding a scroll listener to an element that has overflow-y: scroll

If you have a div:

<div id='my-div'>my div</div>

styled like this:

#my-div {
  overflow-y: scroll
}

The JavaScript code below will not work when scrolling inside the div:

window
  .addEventListener('scroll', function() {
    console.log('do something on scroll')
  })

This is because scroll events do not bubble for performance reasons.

It is better to directly attach the scroll event like this:

document
  .getElementById('my-div')
  .addEventListener('scroll', function() {
    console.log('do something on scroll')
  })

However, passing references to elements in applications can be cumbersome. In my React application (but also applicable to any application following the W3C document spec), there are multiple layers of nesting to access the element with overflow set. Is there a way to avoid passing references around?

Answer №1

Seems like a straightforward solution is to include a true as the third argument in the addEventListener method to capture events from all Elements. For example:

document.addEventListener('scroll', function(e){ }, true);

If you're looking for more detailed information on this issue, there's another helpful thread that discusses it further: Listening to all scroll events on a page

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to implement filtering on a click event in React Three.js?

I recently explored a 2 cubes sample, and I encountered an issue. When clicking on one cube, both it and the cube behind it get clicked at the same time. Is there a way to make sure that only the nearest cube is clicked or hovered? In simpler terms, when ...

"Having trouble with defaultValue in react-hooks-form not functioning properly with NextUi Select component. Can

const fetchUserData = async (userId) => { const userApi = new UserAPI(); const result = await userApi.getInfoById(userId); const userData: Record<string, any> = result.data; const otherData = userData.other; for (const key in ...

What does getsession.insert in the Ace Editor return?

Seeking clarification on the return values of the addMarker and insert functions in ace editor's edit session. The official documentation lacks detail, leaving me uncertain. Refer to the edit session API for more information. I've encountered i ...

Tips for integrating material UI sample snippets into a component

I have a specific goal in mind: to create a page with a header that says "Survey Start" followed by a user selection interface. My vision is to implement a simple component as follows: export class Survey extends Component { state = { } ren ...

Is there a way to position a div on top of the header with flexbox?

I am a beginner in the world of coding and I am currently experimenting with flexbox to create the layout for my website. However, I am facing a challenge where I need to overlay some text and an image on top of the pink rectangular section that is part of ...

Tips for inserting a jQuery snippet into a universal file

Seeking some advice on integrating jQuery code into my JavaScript file, which is located at . The code snippet I am attempting to add looks like this: $(document).ready(function() { queue = new Object; queue.login = false; var $dialog = $ ...

Tips on creating the <th> tag just one time

I have a task that involves dynamically creating cells in a table based on an array of data. However, I am unsure how to also create a header cell for this table within the same function. Is there a way to streamline the process and create the entire table ...

What is the process for transforming specific words within a sentence into clickable links?

In my experience with React, I faced the challenge of creating links within a string. After experimenting with various approaches, I finally discovered a solution without having to manipulate the DOM directly. function generateProfile() { const hashtagRe ...

What is the best way to manage the loading and unloading of JavaScript within dynamically loaded partial pages?

Utilizing jQuery and history.js, I manage seamless transitions between partial pages to prevent entire document reloads. Some of these partial pages include unique javascript files. Despite successful page transitions, remnants of executed javascript linge ...

PHP email not going through, stuck on an empty screen

In the process of creating a PHP form mailer, I have encountered multiple obstacles and none of the versions seem to work. My goal is for the information submitted via the form to be automatically sent through email when the 'Submit' button is cl ...

Tips for transferring a boolean value to a generic parameter in Java?

Looking to pass a boolean value to the Generic type in order to be utilized within a condition. This is the generic type interface OptionTypeBase { [key: string]: any; } type OptionsType<OptionType extends OptionTypeBase> = ReadonlyArray<Opt ...

The final DOM is not loading properly when using Jest and React Testing Library during page initialization

I'm currently working on testing my React application with Jest and React Testing Library. During this process, I am attempting to verify that the login page displays a specific message once it has fully loaded. Upon initial loading of the login page, ...

Submitting forms with Ajax using Jquery

I'm looking to utilize ajax to submit a form in the background. My attempt so far is as follows: <div class="form-horizontal" id="form"> <label for="name" class="control-label">Enter your name</label> <div class="controls ...

Tips for resolving the ExtPay TypeError when using Typscript and Webpack Bundle

I am currently trying to install ExtPay, a payment library for Chrome Extension, from the following link: https://github.com/Glench/ExtPay. I followed the instructions up until step 3 which involved adding ExtPay to background.js. However, I encountered an ...

Looking for guidance on how to initiate or halt React Native Moti animation when the state changes? I've been running into issues where my method appears to disrupt

While experimenting with the Moti animation, everything was working perfectly until I included the playbackState === State.Playing condition. Unfortunately, once I added it, the animation looped only once and then stopped repeating. I'm puzzled as to ...

Using React to establish communication between sibling components

Hey there, I'm a beginner in React and recently created a small application with a single form to practice my skills. This app includes 4 components apart from the App.js file. My main challenge is getting my Checkbox.js component to communicate with ...

What steps should be taken in PHP to display a popup when the user input is found to be invalid?

I've been working on implementing a popup in PHP to show if the user enters an existing username, as per our teacher's requirement to use popUp instead of alert. I have set the visibility property of the popup to "Hidden" in CSS; <div class=&q ...

Having trouble creating a text channel in discord.js

Today I successfully programmed a bot to respond to the "!new" command, but encountered an unexpected issue. The current functionality of the bot creates a channel named "support-1" when prompted with the "!new" command. However, every subsequent use of th ...

Ways to reach the child window using JavaScript from the parent window

Would it be possible for the parent window to retrieve information about the child window in the scenario where a pop-up is triggered via JavaScript upon clicking a link in the parent window? It is common knowledge that elements in the parent window can b ...

What is the reason behind being limited to sending only 5 requests if I fail to heed the data event?

I've come across some related questions while researching this topic, such as Why is node.js only processing six requests at a time?. However, I am still struggling to fully grasp the specifics. Below is a breakdown of my scenario: Firstly, let&apos ...