As the div elements stack up and you scroll towards the top or bottom

I am dealing with two nested DIVs that are both scrollable.

When I scroll the "inside div" (the green one in the attached file), and reach the bottom of the DIV, it starts scrolling the DIV beneath it. How can I prevent the DIV beneath from scrolling only when I scroll the top DIV?

<div class="container">

<div class="neneathDiv">
  blah blah <br>blah blah <br>... (content truncated for brevity)
  <div class="topDiv">
    blah blah <br>blah blah <br>... (content truncated for brevity)
  </div>

</div>

.container{background-color: blue; }
.topDiv{background-color: green; width: 300px; position: absolute; top: 0; 
          right:0; height: 100%; z-index: 99; overflow: auto;}
.neneathDiv{background-color: red; height: 100px;  height: 100%; overflow: auto;}

Here's a Demo showcasing my issue

Answer №1

If you want to see an example, make sure to include jquery in your page:

jQuery(function($) {
    $('.topDiv').on('scroll', function() {
        if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
            $('body').addClass("hide");
        } else {
            $('body').removeClass("hide");
        }
    });
});

https://jsfiddle.net/s4yzpwk9/8/

Answer №2

Are you familiar with using javascript and jQuery?

If so, check out this solution:

Take a look at this fiddle

$(".topDiv").bind( 'mousewheel DOMMouseScroll', function ( e ) {
                    //Capture the original Event
                    var e0 = e.originalEvent,
                    //Determine the scroll movement 
                    delta = e0.wheelDelta ;
                    //Adjust the scrollTop value based on scroll direction
                    this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
                    //Only apply the scroll to the specified element      
                    e.preventDefault();
                    //Prevent the default scroll behavior
                }); 

For more information, visit the original post at: How can I prevent scrolling when reaching the bottom of a div using jQuery?

Answer №3

Give this code a shot, it might just do the trick for you.

$( '.mainContainer' ).on( 'wheel mousewheel', function ( event ) {
var e = event.originalEvent,
    delta = e.wheelDelta || -e.detail;

this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
event.preventDefault();
 });

Answer №4

The situation is behaving correctly because your page extends beyond the viewport. To address this, you can utilize JavaScript to capture the scrolling event and prevent it from affecting a specific element or adjust the structure of your page accordingly.

One approach could involve limiting the height of the body and concealing any overflow.

body, html, .container {padding:0; margin:0; height:100%; max-height:100%;}
.container {overflow: hidden;}

Example implementation can be seen here: https://jsfiddle.net/s4yzpwk9/6/

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

'Dynamic' styling in Next.js using conditional CSS

I am exploring ways to style elements based on their parameters. I have been searching for terms like "conditional css react" and "implementing conditional styles in react with css". Here is a snippet of my code: e.g. <div class="ChatComponent_ ...

Variation in functionality of onclick in javascript

Can you identify the distinction between the two primary lines of JavaScript in this example? HTML: <html> <body> <form> <input type="button" value="one" id="one" /> <input type="button" v ...

Adjusting the transparency of the background without altering its colors

I need help figuring out how to merge custom palettes I've created such as this one, which resulted in a .css and .less file. While I want to retain the colors, I also require certain elements' backgrounds to be transparent. To achieve this, I c ...

Tailwind CSS - when it comes to styling, larger screen media queries are taking precedence over smaller media queries

Currently tackling screen responsiveness issues in a Nextjs + Tailwind CSS project. All Tailwind breakpoints are functioning correctly except for "sm" and below. Snippet of the problematic code: <div className="bg-red-200 sm:bg-white md:bg-gray-70 ...

Eliminate extraneous space with the clearfix:after pseudo-element

Could use some help figuring out how to remove the unwanted whitespace caused by clearing the float (specifically after the tabs). Any suggestions on how to fix this issue? Take a look at the code snippet below (jsfiddle): /* Clearfix */ .clearfix:af ...

The div element is experiencing a resizing issue

I am currently working on the following HTML markup: <div class="card" style="height:100%"> <div class="controlPanelContainer"> <div class="card" style="background-color:yellow;height:200px"> </div> <div class= ...

What is the reason behind using <script> tag for scripts, instead of using <style> tag for external CSS files?

A family member who is new to Web Development posed an interesting question to me. Why do we use <script src="min.js"></script> and <link rel="stylesheet" href="min.css">? Why not simply use <style href="min.css"></style>? Wh ...

Display a pop-up upon clicking a button

I've created a custom popup form using Flodesk and added the corresponding Javascript Snippet to my website just before the closing head tag. <script> (function(w, d, t, h, s, n) { w.FlodeskObject = n; var fn = function() { (w[n] ...

The production build for Angular 9 Keyvalues pipe fails to compile

After successfully running my app on the development server with ng serve, I encountered a failure when trying to build it for production. The error message that popped up was: ERROR in src/app/leaderboard/leaderboard.component.html(9,17): Argument of typ ...

Effortlessly Styling Children Elements in Emotion JS When Parent Element is Hovered

This particular query seems to have been posed and resolved in various ways, but the solutions I've come across either do not pertain to Emotion or the responses related to Emotion haven't yielded results for me. I am currently using @emtion/[ema ...

Full-width sub menu within the menu

I'm trying to make a sub menu appear below my main menu that is the same width as the main menu, which has a fixed width. I want the sub menu to adjust its width based on the number of links it contains. Is this even possible? Here's the code I h ...

An error occurs when attempting to assign a value to a MUI file TextField

Struggling with setting the value of a MUI Textfield that has type="file" props, resulting in the following exception being thrown: Uncaught DOMException: An attempt was made to use an object that is not, or is no longer, usable Interest ...

Align the top navigation centered horizontally, with the logo on the left and language selection on the right

Is there a way to center my menu horizontally while keeping the logo aligned to the left and language selection to the right, all with consistent proportions when resizing? I've tried various suggestions but nothing seems to work for my navigation bar ...

Is it possible to automatically select a file and upload it to a specific server? If so, how can this

Hey there! I'm looking for a code snippet that allows me to automatically select a file and upload it to a specific link. Any ideas on how to accomplish this task? Here is the HTML code: <html> <head><title>Uploading</title>& ...

The navigation toggler seems to be malfunctioning in the browser, but it is functioning properly on Code

I'm having an issue with my navigation bar toggler. The code works fine in Codeply, but not in the browser. The hamburger button appears on mobile, but the lists are not showing up. Here is the code snippet I'm using: <html> <head> ...

Exploring the usage of slots in WebComponents without relying on shadow DOM

I am working on developing a WebComponent without utilizing ShadowDOM. So far, everything has been going smoothly. However, I now aim to create a component that wraps other components similar to how it is done in Angular with @ViewChild / @ViewChildren. (I ...

Ways to create a clickable red button

I'm working with bootstrap, CSS, and particle.js for this project. I have also linked a custom CSS file here. However, when I apply bootstrap and particle.js, the red color generate password button becomes unclickable. After checking in Chrome dev too ...

IE8 is not properly handling nested HTML elements that have the display:none property set

I am currently using jQuery to create a smooth fade-in effect for an <article> element that contains a <section> element. The outer element has CSS properties of display:none, position:fixed, and z-index:5. Meanwhile, the inner element is styl ...

Achieve button highlighting by hovering over the card - Bootstrap 5

Is there a way to make a button focus when hovering over a card in Bootstrap from any place on the card? Currently, the button only focuses when directly hovered over. I want to achieve the effect shown in the second image after hovering over any part of t ...

Guide to collapsing all menus in an Angular 4 Accordion

I am a beginner with Angular 4 and I have implemented an accordion with the structure of Categories, Groups, and Subgroups. When I click on a category, it displays all the groups within that category. Similarly, when I click on a group, it shows all the s ...