Tips for preventing scrollbar overlap

I am experiencing an issue with two scrollbars in my HTML file. Scrollbar a is short, while scrollbar b is larger. When I scroll to the end of Scrollbar a, the focus shifts to Scrollbar b and it starts scrolling. Is there a way to prevent this from happening? I want the mouse to only interact with Scrollbar a when it is over that specific scrollbar, not Scrollbar b.

document.getElementById('left').onmousewheel = function(e, d) { 
   if((this.scrollTop === (this.scrollHeight - this.offsetHeight) && d < 0)
    || (this.scrollTop === 0 && d > 0)) {
     e.preventDefault();
   }
}
html, body { height: 100vh; margin: 0; }

.container {
  width: 100vw;
  height: 100vh;
  background: aqua;
  margin: auto;
  padding: 1px;
}

#right {
  margin-left: 20vw;
  height: 200vh;
  background: yellow; 
}

.static {
  height: 50px;
  background: green;
}

#left {
  width: 20vw;  
  background: red;
  float: left;
  position: fixed;
}

#middle {
  height: calc(100vh - 15px);
  overflow-y: scroll;
}
<section class="container">
  <div id="left">  
    <div id="middle">
      <h2>Left</h2>
      <p style="height: 9001px;">Lorem ipsum</p>
      <p>Lorem ipsum</p>
    </div>  
  </div>
  <div id="right" class="scroll">
    <h2>Right</h2>
    <p style="height: 300px;">Lorem ipsum</p>
    <p>Lorem ipsum</p>
  </div>
</section>

After scrolling in left, if the mouse remains on left and scrolling resumes, then right will also start scrolling. I'm exploring possibilities of achieving this behavior purely with CSS without resorting to Javascript.

Thank you!

Answer №1

To avoid the parent element from scrolling when the inner element reaches the bottom of its scroll, you can use the following JavaScript code:

document.getElementById('left').onmousewheel = function(e, d) { 
   if((this.scrollTop === (this.scrollHeight - this.offsetHeight) && d < 0)
    || (this.scrollTop === 0 && d > 0)) {
     e.preventDefault();
   }
}

Alternatively, you can also achieve the same effect by adding the following CSS:

section {  
 overscroll-behavior: none;
}

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

Generating Components Dynamically in Angular 4 Based on User-Defined Input

I am interested in developing an innovative App using Angular 4 that enables the creation of components from various storage sources such as database, URLs, and server-stored files. The main objective is to allow users to design their own components for pe ...

Does the Width Toggle Animation fail to function in FireFox when using jQuery?

Has anyone else encountered this issue with the jQuery animate function not working in Firefox but working fine in IE8, Opera, and Chrome? I'm experiencing this problem and wondering if there's a workaround to make it work in Firefox as well. ht ...

Using CSS syntax to target a class within an element distinguished by its unique id

Consider the following code snippet: <div id="dogs" class="content">hello</div> <div id="frogs" class="content">hello</div> <div id="hogs" class="content">hello</div> <div id="logs" class="content">hello</div&g ...

What is the best way to incorporate classes into <li> elements within WordPress?

I'm currently in the process of building my WordPress theme from scratch, but I've run into a roadblock when it comes to adding custom classes to the < li > tags alongside those that are automatically generated by WordPress. Below is the co ...

Troubles with React Material-UI class names overlapping in library integration

Incorporated a library into our Creatives project that functions similarly to one of our existing pages. Upon interacting with a graph, I observed the generation of numerous empty <style data-jss> tags which override the current styling on the page. ...

The functionality of redirecting to the homepage after registration is not working as intended due to the issue of the uiD not being properly set in the session

I have created a registration form to register users and once the registration is complete, it should redirect to index.html (home page). There seems to be an issue where upon pressing the submit button, the page refreshes and the form resets without redi ...

Is Material UI equipped to handle CSS Container Queries?

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries Recently introduced container queries for CSS are an exciting development. I'm curious if MUI 5.0 already supports them out of the box. It seems that the current SxProps do not in ...

Angular's Spanning Powers

How can I make a button call different methods when "Select" or "Change" is clicked? <button type="button" class="btn btn-default" *ngIf="!edit" class="btn btn-default"> <span *ngIf="isNullOrUndefined(class?.classForeignId)">Select</spa ...

Capture the selected hyperlink and show the corresponding page title in a designated box for reference

I want to track the links that users click on and display them in a box with an image and name of the page. Additionally, I would like to show how long the user spent on each page below the image. The images in the box should also be clickable links to the ...

Span element not displaying as a clickable hyperlink

The magnifying glass icon inside the orange search bar at the top of this page is not appearing as a link, so when hovered over there is no pointer.. Any thoughts on why this might be? Below is the CSS: .searchGlass { background-image: url(/images/search ...

Various Ways to Add Hyperlinks in HTML Using CSS Styles

Does anyone know how I can link multiple HTML files to one hyperlink, where only one file opens randomly when clicked? I am currently using . Your assistance would be greatly appreciated! I've tried searching online for a solution, but haven't ...

ng-bootstrap's accordion imparts unique style to its child icons

https://i.sstatic.net/YzmtN.png Visible star icon indicates lack of focus on the accordion https://i.sstatic.net/aNW31.png Star disappears when accordion gains focus Below is the CSS code I used to position the star to the left of the accordion: .icon ...

Learn how to toggle between two different image sources with a single click event in JavaScript

If the button is clicked, I want to change the image source to one thing. If it's clicked again, it should change back to what it was before. It's almost like a toggle effect controlled by the button. I've attempted some code that didn&apos ...

Display the text field in Angular with the appearance of a password field, not an input field

I am currently working with Angular 8 for my Angular-Electron project. Within this application, I have a sensitive field labeled API-Key stored in tabular format that needs to be displayed on the user's account page. However, it must appear as a passw ...

Modifying the attribute from "content-disposition:attachment" to "content-disposition:inline"

I created an HTML email signature that worked perfectly until I tested it in the Outlook Mail Client 2010, which is currently not displaying images but instead offering them as download attachments. I am utilizing DataUri for images: <img width="56" ...

Incorporating Next and Previous navigation buttons to my slideshow

I'm looking to enhance my image slider by including next and previous buttons, but I'm unsure of how to proceed. Can anyone provide guidance or assistance with this? Below is the basic structure that I currently have in place: Here is the code s ...

Tips for changing the dimensions of the canvas?

I'm struggling to display a photo captured from the webcam. The sizes are not matching up, and I can't pinpoint where the size is defined in the code. https://i.stack.imgur.com/Is921.png The camera view is on the left, while the photo should be ...

Improving Performance of a Large Unordered List using JavaScript

My website currently features a search box that retrieves images and displays them in a list format. Each image has an associated click event that triggers an overlay on the parent li element when clicked. However, with search results exceeding 300 images ...

Methods for incorporating rtl functionality into Angular Chosen

I am currently using Angular with Chosen (source) and I am attempting to implement right-to-left (RTL) support. Here is my demo, but despite referencing resources, I am encountering issues. The main problem arises when the body element has a dir="rtl" att ...

What causes a space to appear at the bottom of the screen when elements exceed the height of the screen?

https://i.sstatic.net/jo7W5.png https://i.sstatic.net/Au7OW.png After passing the screen height, I noticed a sudden margin-end appearing. I've tried using '''cover: 100% 100''', but the image ends up shrinking. As my fr ...