Tips for ensuring that scrolling is not impeded by a child div that has its own scroll functionality

Just a quick question, but I have a large body that is scrollable, and inside that body I have several child divs that are also scrollable.

As I scroll through the body, sometimes my cursor will hit one of these child divs and get stuck scrolling that instead. It can be frustrating because I can only continue scrolling the parent body once I reach the end of the scrollable area in the child div.

Is there a way to prevent scrolling in the child div when I'm trying to scroll the parent body? If not, maybe there's a way to only allow scrolling in the child div when the mouse button is pressed inside it, otherwise the parent body should scroll.

For reference, here's an example of this behavior: JSFiddle

The positions are fixed using position: absolute

.area{
    overflow:auto;
    max-height:250px;
    width:300px;
    background:#000;
    color:#FFF;
    position: absolute;
    bottom:0px;
}

Answer №1

Consider implementing something similar to the following code snippet. If the user has not interacted with the specified div, scrolling will occur on the main document instead of the div.

var scrollArea = false;
$( '.area' ).on( 'mousewheel DOMMouseScroll', function ( e ) {
    var event = e.originalEvent;
    var d = -event.wheelDelta || event.detail;
    if(scrollArea)
    {
       this.scrollTop += d;
    }
    else
    {
     $(document).scrollTop($(document).scrollTop()+d);
    }
    e.preventDefault();
});

$( '.area' ).on( 'click', function ( e ) {
  scrollArea = !scrollArea;
});
.area{
    overflow:auto;
    max-height:250px;
    width:300px;
    background:#000;
    color:#FFF;
    position: absolute;
    bottom:0px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="area">you thoroughly searched for an answer before asking your question? Sharing your research helps everyone. Tell us what you found (on this site or elsewhere) and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and above all, it helps you get a more specific and relevant answer!
Have you thoroughly searched for an answer before asking your question? Sharing your research helps everyone. Tell us what you found (on this site or elsewhere) and why it didn’t meet your needs.

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

Selecting a webpage from a dropdown list to display its specific content within an iframe

I have a dropdown menu similar to this: <form name="change"> <SELECT NAME="options" ONCHANGE="document.getElementById('frame1').src = this.options[this.selectedIndex].value"> <option value="">Choose</option> <opti ...

Exploring the Matrix Filter Functionality in JavaScript

After successfully setting up CSS rotation for all browsers, I am now looking to achieve the same effect using JavaScript. jQuery is also being utilized in this process with the following code snippet: .css({ '-ms-filter':"progid:DXImageTransfor ...

Adjusting the height of a textarea with javascript

I am trying to reset the contents and height of an auto resizing textarea element. My attempts so far include: document.getElementById('textarea').value = ''; As well as: document.getElementById('textarea').attribute(&apos ...

What is causing the ajax code to malfunction?

I am new to ASP MVC and trying to create a login form using AJAX. I have written a JsonResult in the controller to check the username and password, but for some reason it is not working. Here is my controller code: public ActionResult login() { ...

The impact of jQuery ajax on SVG data transmitted to a ColdFusion server appears to be significant

While using $.ajax() to send a string to a coldfusion server for storage in a table, I encountered an issue. Upon querying and attempting to use the data later on, an error message appeared stating "null Enclosed Exception: Invalid byte 2 of 3-byte UTF-8 s ...

A step-by-step guide on replicating the Google login form animation

Help needed here! I'm attempting to recreate the user login page for Google Chrome, and I've hit a roadblock (seriously, I don't even know where to start) with the input field of that form. I have tried inspecting Chrome's page, but it ...

Is there a advantage to using Angular's component style encapsulation for improved performance?

Recently, I came across the styling methods of Angular 2 which allows you to directly include your styles within the component. If you want to explore more on this topic, check out loading styles. There are two options for style encapsulation in Angular 2 ...

JavaScript Click Event Not Functioning

I am attempting to create an interactive feature where clicking on any image will either reveal a clear version of the picture if it is blurred, or blur the picture if it is clear. The current issue I am facing is that when I click on a blurred image, it ...

What is the proper way to structure an Xpath search?

Recently, I've been working with Selenium in QA Test automation. When it comes to selecting/writing Xpath for an element, I've noticed that there are different formats that can be used. For example, protected final String LOGIN_BUTTON_LOCATOR = ...

When working with express.js, the following error may occur: Uncaught Error [ERR_HTTP_HEADERS_SENT]: Unable to modify headers after they have been sent to the client

Seeking assistance for resolving this issue. The purpose of this code is: Update the artist using the specified artist ID and data from the request body, then save it to the database. Return a 200 response with the updated artist in the response body. If ...

The jQuery pop-up fails to activate on the initial click

I have multiple "Buy Now" buttons for different products. If the button is labeled as "sold-out," it should not do anything, but if it's available, it should trigger a jQuery Magnific Popup. Currently, the popup only opens after the second click becau ...

Utilizing Local Storage in Vuex Store with Vue.js

I have been working with localStorage for storing and retrieving items in my JavaScript code housed within a .vue file. However, I am now looking to find a way to transfer this stored data into my Vuex store, specifically within the mutations section locat ...

Convert lengthy URLs in PHP into multiple lines

I need to display a lengthy string within a cell in a table, but the URL is too long. How can I convert something like this: /vlive.qqvideo.tc.qq.com/u0020mkrnds.p1203.1.mp4?vkey=7AB139BF6B32F537 47E8FF192E6FE557B3A3D644C034E34BF6EAEB4E0774F2A92EF3AC5C0075 ...

Is it possible to prevent a line break in a div tag, or are there other options available?

On my ASP.NET 4 / VB website, I encountered a scenario where I needed to incorporate a class called "noprint" in my footer, as specified in the print.css file. However, there was already a span class present, so I ended up enclosing div tags around it. Mor ...

Receiving alerts as soon as the page DOM is fully loaded, prior to the window.onload event

Is there a consistent way to get notified when the page body has loaded across all browsers, regardless of differences in implementation? Here are some methods I'm aware of: DOMContentLoaded: Supported in Mozilla, Opera 9, and newer WebKits. This i ...

Is there a way to instruct npm to compile a module during installation using the dependencies of the parent project?

I am curious about how npm modules are built during installation. Let me give you an example: When I check the material-ui npm module sources on GitHub, I see the source files but no built files. However, when I look at my project's node_modules/mate ...

Check List Table A, Locate Table B, and Apply the Findings to Update Table A

My dissertation requires me to classify images. The image links are stored in the "ocr" table. On my search page, I display all the images in rows. Using AJAX, I search the "diss_kategorien" table and display the results in a dropdown within the OCR table. ...

Create HTML code automatically from JSON data

Currently, I am in the process of developing a template system. The concept is that an ordinary user can create a JSON file which will then be used by the system to automatically generate HTML code. However, I am facing some challenges and feeling a bit lo ...

Actility's LoraWan platform integrated with Node-Red

I am currently working on learning how to implement an HTTP GET request for the Actility platform in Node-RED. However, I keep encountering an error 401 indicating that the authorization bearer is missing. This is how my setup looks: https://i.sstatic.ne ...

Encountering an error when using Angular Material virtual scroll in conjunction with Angular-UI

Trying to incorporate Angular material's virtual scroll feature on angular-ui-tree is proving to be a bit challenging. The error message that keeps popping up is: Controller 'uiTree', which is required by directive 'uiTreeNode', ...