Handling mousewheel events for child elements and their parent element

I developed a custom jQuery plugin that replaces the default scrollbar with my own, managing mousewheel and bar dragging events.

The issue arises when I place content containing my custom scrollbar within another content also using my scrollbar. When I scroll using the mousewheel on the child content, it causes the parent content to scroll as well.

This occurs because I attached a mousewheel event listener to both the child and parent elements, resulting in triggering of both event handlers when the mouse moves over them simultaneously.

My dilemma is how to enable scrolling only on the child content without affecting the parent element.

If you have any advice on how to tackle this problem, I would greatly appreciate it. The dragging scrollbar functionality seems to be working fine.

Answer №1

To prevent the event from propagating further, you need to stop its propagation. This will ensure that the event does not bubble up the DOM tree and trigger on parent elements.

Check out this link for more information

$('body').on('mousewheel', function (e) {
  alert('Body scroll');
});

$('.child').on('mousewheel', function (e) {
  e.stopPropagation();

  alert('Child scroll only');
});

If you remove the e.stopPropagation(); line, you'll see that both alerts will be triggered.

For more details:

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

Is there a way to show a fallback message for unsupported video file formats?

When incorporating a video element on my webpage, I typically use the following code: <video src="some source" controls> Error message </video> Based on my knowledge, the "Error message" will only appear if the browser does not support the ...

"Send the response in ExpressJS before making a request to loop through the

I am currently working with a postgres database that contains records with a column format in JSON, which refers to other similar records. The challenge I am facing is retrieving linked records through asynchronous methods due to the nested structure and ...

ReactJS is having trouble with inline animation styles

After successfully implementing a timer using CSS with animation, everything was working as expected. However, I encountered an issue when trying to convert it into JSX inline styles in React. The animation stopped working as smoothly as before due to the ...

Refreshing a form following an ajax call using the ajaxForm method

I have implemented the following code to submit my form using ajaxForm plugin: $('#donations').ajaxForm({ dataType: 'json', beforeSubmit: function(){ $('#message').html(''); ...

Combining several btn-group and btn-group-vertical elements within each other

I am looking to create a button grid resembling a numpad for my project. I am utilizing angular and bootstrap 5 and thought of using btn-group and btn-group-vertical for this purpose. While this setup would work for a regular button grid, I need to have t ...

Retrieve a HTML element's tag using jQuery

I have a search bar with two tabs to choose from for the type of search. I am looking to assign the .active class to the list item whose search_type matches a parameter from a request (e.g., params[request_type]). <ul data-tabs="tabs" class="tabs"> ...

Flexible DIVs with a maximum width of 50% that adjust to different

I'm having trouble getting these two DIVs to display properly within a parent DIV while maintaining responsiveness. I want them to each take up 50% of the screen with a 10px margin between them. Does my current code approach seem correct? .box-cont ...

Eliminable Chips feature in the Material UI Multiple Select component

The Material UI documentation showcases a multiple select example where the selected options are displayed using the Chip component and the renderValue prop on the Select. By default, clicking on the current value opens the list of available options. I am ...

Navigation is failing to float in the correct position

Having issues with my navigation positioning, specifically when it reaches the breakpoint. There's a strange gap on the right side that I can't seem to fix by adjusting padding or margin settings. I'm relatively new to this so please bear wi ...

HTML tag data search using Regex

I received an HTTP request and I'm attempting to extract specific data from it using a regular expression. For instance, in this particular part of the HTML: <tr><th>Continent:</th><td class='trc'>Europe (EU)</td& ...

Activate a spinner when a button is clicked within a row of an antd table

I created a table with a column that includes a button like the one below: const columns = [ ... { title: "button", dataIndex: "key", render: (text, record) => { return ( <Button icon={<Del ...

nodemon was not properly installed on the system

I attempted to add nodemon to my application using the command below: npm install nodemon -g This is the output that was generated: changed 116 packages, and audited 117 packages in 8s 16 packages are looking for funding run `npm fund` for details fou ...

Issues with jQuery Ajax functionality in Rails application not achieving successful completion

When a card is moved onto another stack, an Ajax call is triggered twice. This only happens when there are multiple stacks. However, if the cards are rearranged within the same stack, the call is triggered only once. The Ajax call sends an array of IDs fo ...

jQuery counter no longer updates when scrolling

I'm having trouble with my jQuery counting function when using the scroll feature on a specific div ID. The numbers freeze if I keep scrolling before they finish updating on the screen. Whenever I scroll to the defined div ID, the counting function k ...

Tips for sending parameters to XSLT using a Javascript function

Despite my efforts to find a solution in various online posts, I haven't been able to resolve the issue. The challenge lies in my HTML file that includes a JavaScript function for parsing XML and rendering XSLT. I have multiple 'records' in ...

Include a Class into a label using jQuery

I am having an issue where I need to specifically add the error class to a <label> with the name attribute set to choice_16_0. However, when I try to achieve this using my code, it ends up changing all labels on the page to <label for="choice_16_0 ...

Unable to drag and drop onto every part of a div element that has undergone a CSS transformation

Having trouble implementing a Drag & Drop feature. The droppable div is styled with CSS transformations, but the draggable div doesn't drop correctly on the right part of the box. If you'd like to take a look at the code and try it out yourself, ...

The autocomplete feature in Laravel that relies on Ajax is sluggish

My form has an autocomplete feature that is functioning slowly. Even with an average typing speed, I can type faster than the suggestions load. The feature utilizes jQuery to make an ajax call to a URL, which triggers a function in a controller to generate ...

Encountering a Vercel deployment failure due to a TypeError: The 'split' property cannot be read from undefined within several objects

I'm having trouble deploying my web application for the first time and encountering this error on Vercel: TypeError: Cannot read property 'split' of undefined at Object.3qS3 (/vercel/path0/.next/serverless/pages/[collection]/[templateId].j ...

Selenium Timeout Error: Locator Not Found

Once again, I am struggling with the locator and trying to figure out the correct syntax and code. Here is the code I was attempting to execute: wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='col-xs-6']//input[@class=&a ...