What steps can I take to stop the window/body from scrolling "through" a modal div?

Let me simplify my layout for you:

<body>
    [... lots of content ...]
    <div id="modal-overlay">
    </div>
</body>

The body has so much content that the whole page scrolls.

The styling for #modal-overlay looks like this:

#modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

When a user performs an action, I show the #modal-overlay by changing display: block;

Now, the #modal-overlay fills the entire viewport.

But here's the problem...

Sometimes, when you swipe up or down on the #modal-overlay - its content scrolls as expected.

However, at other times, the body ends up scrolling instead, and the content within the #modal-overlay remains stationary. It's like I'm scrolling the body "through" the #modal-overlay, which is exactly what I want to avoid.

It seems completely random whether the swipe gesture scrolls the #modal-overlay or the body.

I've come across a few solutions (such as adding overflow:hidden to the body), but I'm hesitant to try them as they mess up the scroll position and cause other issues. I'm looking for a fix that works regardless of how many layers are nested. My goal is to prevent scrolling "through" the topmost layer, without tinkering with the layers beneath it.

This scrolling issue is especially problematic on iOS, as scrolling the body shrinks the browser chrome, expanding the viewport and disrupting the layout of the #modal-overlay which is sized to fit the viewport. Frustrating, isn't it?

Thank you in advance for any suggestions or help!

Answer №1

While I haven't personally tested this on an iOS device, one possible solution could be to experiment with the slimscroll jQuery plugin if you're working with multiple layers and need to prevent scrolling on certain layers. Give it a try and see if it works for your needs. Good luck!

Answer №2

Body should have overflow-hidden and dynamically include

position: absolute; width: 100%; height: 100%
CSS properties to the content wrapper (content container inside the body) when the modal is opened.

When the Modal is open

  <style>
   .modal-open {
     overflow: hidden;
   }

   .modal-open .container {
     position: absolute;
     width: 100%;
     height: 100%;
   }
  </style>

<body class="modal-open">

  <section class="container">
     Site content goes here
  </section>

  <div class="modal">
     Modal content goes here
  </div>

</body>

Add the "modal-open" class dynamically when the modal is opened and remove it when it is closed.

Answer №3

When the modal-overlay is set to cover the entire body at 100%, incorporating position:fixed into the body display when showing the #modal-overlay should resolve the problem.

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

Could anybody provide some assistance with JavaScript?

<div id="toggler" class="toggler"> <div id="toggler" class="line1"></div> <div id="toggler" class="line2"></div> <div id="toggler" class=& ...

Utilizing only JavaScript to parse JSON data

I couldn't find a similar question that was detailed enough. Currently, I have an ajax call that accesses a php page and receives the response: echo json_encode($cUrl_c->temp_results); This response looks something like this: {"key":"value", "k ...

Exploring a collection of objects in an Angular 2 component

Can someone please assist me in identifying what I am doing wrong or what is missing? I keep getting an undefined value for `this.ack.length`. this._activeChannelService.requestChannelChange(this.selectedchannel.channelName) .subscribe( ...

Node.js version 12.7 does not have the ability to support dynamic keys within objects

According to what I've read about ecma6, it should allow for dynamic key objects. I recently upgraded my node to version 0.12.7, but I'm still encountering an error. node /var/www/games/node_modules/app.js /var/www/games/node_modules/app.js ...

Is it possible for JavaScript to interface with MySQL databases?

Is it possible to establish a connection between JavaScript and MySQL? If yes, what is the process? ...

What is the best way to transform a GET request with a query string into a promise?

After successfully making a request with querystring params, I encountered an issue while trying to promisify my request: // Works var Promise = require("bluebird"); var request = Promise.promisifyAll(require("request")); request.get({ url: 'htt ...

The Infinite scroll feature in Fuel UX is ineffective when a specific height is not defined for the

In my current project, I am utilizing Bootstrap and Fuel UX v3.4.0 with the goal of implementing Infinite scroll throughout my entire page. Unfortunately, I have encountered difficulties in achieving this implementation. By replicating the infinite scroll ...

Develop an AJAX script for processing multiple form inputs in PHP and HTML

I am working on a code that involves multiple form submissions, but the submit functionality is not working due to having multiple submits on one page. I have realized that I need an Ajax script to handle this, but I am new to it. Can someone provide me wi ...

"Utilize Ajax to dynamically generate options in a dropdown menu with multiple

I am having trouble populating my multiple dropdown list using AJAX. The dropdown list is dependent on another single selection dropdown list. Here is the HTML code: <div class="form-group"> <label for="InputGender">Select Course</ ...

How can I send and retrieve multiple variables in a URL using WordPress?

There seems to be an issue where I can only retrieve the first variable, specifically "product_category," from the URL http://localhost/coffeesite/?product_category=coffee&brand=bourbon. When I output JavaScript to confirm that the variables are set, ...

Incorporate concealed image into HTML

When I perform actions with a couple of images, I encounter delays because the browser needs to load the images. For example, if I use $('body').append(''); everything works smoothly without any delays. However, when I try using style= ...

Click event inside a repeat loop

Working with AngularJS, I have a collection of colors that each have a specific title and type. These colors are displayed in a list format on the webpage. Now, I am looking to enhance this by incorporating a menu option that allows users to filter and vi ...

Sharing information through a hyperlink to a Bootstrap popup window

I've been attempting to send data to a Bootstrap modal window with no success so far. First, I tried: <a href="#TestModal?data=testdata" role="button" class="btn" data-toggle="modal">Test</a> However, the modal did not open as expected. ...

Error: Material-UI prop type validation failed. Please specify either children, image, src, or component prop for CardMedia component. This error occurred at

I encountered an issue while trying to utilize the CardMedia component from Material-ui. The error message received was 'Failed prop type: Material-UI: Either children, image, src, or component prop must be specified. at CardMedia'. I attempted v ...

What is the best way to insert a new item into an array that is nested within an object?

Currently, I am delving into the world of using $resource in angularjs and finding great examples in this answer AngularJS $resource RESTful example. Fetching and creating records is working fine, but now my challenge lies in adding a "section" to an exist ...

"Switching Classes with a Click Event: A Step-by-

This script is designed to work with SoundCloud's widget in order to enable remote text buttons. I am attempting to modify it so that it functions as a remote for an image button that toggles between different pictures for pause and play, rather than ...

When utilizing JavaScript to input text, I have observed that if I enter text in one text box, any previously entered value is automatically deleted

Currently, I am facing an issue with 3 text boxes in a row that I am populating using JavaScript. The problem arises when I enter text into one field and then move to the second box to input text - the value from the first text box gets removed. Below is ...

Extracting data from a JSON object using Angular

Recently, I've been delving into the world of AngularJS and encountered a hurdle with LocalStorage. After spending numerous hours trying to figure out how to save data locally, I believe I have finally got it working as intended. Now, my next challeng ...

Tips for incorporating images as radio buttons

Can anyone assist me in setting up a straightforward enabled/disabled radio button grouping on my form? My idea is to utilize an image of a check mark for the enabled option and an X for disabled. I would like the unselected element to appear grayed out ...

Tips for preventing the sidebar from extending beyond the footer

I am facing an issue with my sidebar that follows as I scroll. How can I make it stop following when it reaches the footer? Here's what I have attempted so far: $(function() { var floatPosition = parseInt($("#left_menu").css('top')); ...