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

Express.js and Node version 0.10.29: The Mystery Post

Having trouble with sending JSON data to an express server and receiving 'undefined' for req.body.name. This is how the configuration is set up: const express = require('express'); const app = express(); app.configure(function(){ ...

Unable to utilize routes in Express.JS when integrated with nginx

After setting up nginx in front of Express.JS, everything seemed to be running smoothly. However, I encountered an issue when trying to access website.com/users, which resulted in a 404 Not Found error. It appears that accessing website.com works fine, but ...

Using private members to create getter and setter in TypeScript

Recently, I developed a unique auto getter and setter in JavaScript which you can view here. However, I am currently unsure of how to implement this functionality in TypeScript. I am interested in creating an Object Oriented version of this feature if it ...

Executing asynchronous promises within an asynchronous promise using the .join() method

I am currently utilizing node/express, MySQL, and Bluebird for my project. When handling client requests, I am performing async database operations. After the initial database operation callback, I need to carry out some calculations before executing two ...

Change the body selector in CssResource to apply custom styles

I have implemented a CssResource within a ClientBundle following the guidelines provided in the documentation at this link. To access the styles from my UiBinder xml, I have used ui:with as explained in the documentation available at this guide. While th ...

Newly included JavaScript file displays on view page, but triggers a 404 error when attempting to open

Once I implemented the following code in the child theme's function.php file: add_action('wp_enqueue_scripts', 'js_files'); function js_files() { wp_register_script('ajax_call_mkto', get_template_directory_uri() . ' ...

Why is JSP compilation essential in achieving its goals?

At the moment, I am involved in modifying JSP and subsequently uploading it to the server for compilation. Upon compilation, a .class file is generated for the modified JSP. If I were to remove the original JSP from the server, would it still function pr ...

Give properties to a function that is being spread inside an object

While working with React, I am facing a challenge of passing props from the instanced component into the mockFn function. The code example below is extracted and incorporates Material UI, but I am struggling on how to structure it in order to have access t ...

Combine arrays of JSON data within a JSON object using JavaScript

Looking for help with reformatting a JSON response {"result":[["abc","de"],["fgh"],["ij","kl"]]} Interested in transforming the response to: {"result":["abc","de","fgh","ij","kl"]} What's the best way to accomplish this task? ...

The sizing of Bootstrap Inline dropdown menus is not accurate

I want to create two dropdown menus using Bootstrap v3 that are displayed side by side with a specific width and utilizing the grid system. However, I am facing an issue where the width remains the same. Here is an example of what I mean: .menuList > ...

Implementing a variety of threshold colors in Highcharts

Exploring this Highcharts fiddler: http://jsfiddle.net/YWVHx/97/ I'm attempting to create a similar chart but encountering some issues. The fiddler I'm currently working on is here: (check out the edit below!) The key difference in functionalit ...

What is the process for sending the selected checkbox text to an action result on a Razor page?

Working with asp.net core razor pages, I am passing the selected id as an array successfully. However, the issue arises when the selected text value for the checkbox is not being passed or displayed. The problem seems to be occurring on this line of code ...

What is the best approach to managing this scenario where the document is ready?

Just a quick question. I have several JavaScript functions written in this format: var app={ start:function(){ //do something and call calculate }, //end start calculate:function(){ //do more stuff } //end calculate }; //en ...

style of placing a space between objects

Is there a way to create space between the border (underline hover effect) and have the color of the line be red? a { text-decoration: none } li { display: inline; } li:hover { text-decoration: underline; } <li><a href=""> abc < ...

What is the best way to add flair to the HTML <title> tag?

Just a quick question - I'm wondering if there is a method to apply CSS styles to an HTML element. Here's an example declaration: title { color: red; font-family: 'Roboto', sans-serif; } ...

Ways to prevent my website from being accessed through the Uc Browser

Is there a way to prevent my website from functioning on UC Browser using HTML or JavaScript? ...

How to position child divs at the center of a parent div

I am looking to set the maximum width of the "circlecontainer" to 300px, but I also want my 4 child divs (each with a max width of 300px) to remain centered in the browser when it exceeds a width of 1200px. Currently, they are aligned to the left side. Be ...

Why do Jquery Ajax calls stop working after the first successful call?

Currently, I am utilizing jQuery to create a personalized email system which features a left panel displaying a list of messages and a right side showing the selected or current message. However, after performing an Ajax call (such as deleting a message an ...

What is the best way to define these variables at a global scope within my controller?

In my controller (NodeJS/Express), I have 10 routes that all use the same variables 'startDate' and 'endDate'. Is there a way to declare these globally so they don't need to be repeated in each route? Currently, my code checks if ...

What could be causing npm to not recognize my module in the "require" statement?

Currently, I am in the process of developing an npm module and conducting tests before making it available to the public. The method I am following is outlined in a blog post found at this link. However, I am encountering difficulties when trying to requir ...