Two scrollbars within Bootstrap-4 modal

$("div[id^='entry']").each(function(){
  
  var currentModal = $(this);
  
  //click next
  currentModal.find('.btn-next').click(function(){

    currentModal.modal('hide');
    currentModal.closest("div[id^='entry']").nextAll("div[id^='entry']").first().modal('show'); 
     
  });
  
  //click prev
  currentModal.find('.btn-prev').click(function(){
 
    currentModal.modal('hide');
    currentModal.closest("div[id^='entry']").prevAll("div[id^='entry']").first().modal('show'); 

  });

});
    body.animsition.modal-open,body.animsition{
        padding-right: 0!important;
            overflow: hidden!important;

    }
    .modal.fade.show {
        overflow-x: hidden;
        overflow-y: auto;
    }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
     <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#data1">
                   View
                   </button>
                   <p>If you don't want to download and host Bootstrap 4 yourself, you can include it from a CDN (Content Delivery Network).

MaxCDN provides CDN support for Bootstrap's CSS and JavaScript. You must also include jQuery:If you don't want to download and host Bootstrap 4 yourself, you can include it from a CDN (Content Delivery Network).

Max...

I'm experiencing an issue with double scrolling while using the Bootstrap 4 modal. I've attempted various solutions using jQuery and CSS, but have not been able to achieve the desired outcome.

https://i.sstatic.net/yVw1w.png

There are two scrollbars present in the situation. Even after adjusting the body's overflow to auto when the modal is closed and hiding the overflow when the modal is open, one scrollbar appears and shifts the body content to the side. When multiple Bootstrap modals are triggered, the modal-open class on the body disappears.

Answer №1

Here's my solution to the problem at hand. I have implemented a JavaScript function to toggle the class modal-open on the html element whenever the modal is opened or closed.

jQuery('.modal').on('shown.bs.modal', function (e) {
    jQuery("html").addClass("modal-open");
});
jQuery('.modal').on('hide.bs.modal', function (e) {
    jQuery("html").removeClass("modal-open");
});

In my case, adding the modal-open class to the html element ensures that the page has a overflow: hidden; style, effectively hiding the scrollbars.

Answer №2

It appears that the issue lies with the "modal-open" class not always being added to the body when opening the next modal. This may be due to the simultaneous execution of your hide() and show() actions.

The class responsible for removing the scrollbar from the body is:

.modal-open {
    overflow: hidden;
}

One potential solution without modifying your functions involves instructing the computer to add the class modal-open to <body> whenever a modal is displayed on the page. This can be achieved using modal events. More information can be found in the documentation here: https://getbootstrap.com/docs/4.0/components/modal/

The following snippet needs to be added to your script: no additional CSS modifications are required:

$('.modal').on('shown.bs.modal', function (e) {
  $("body").addClass("modal-open");
});

/* Your JavaScript code snippet with added functionality for next and previous buttons */
 /* Example HTML code for modals */ 

EDIT 1:

After some clean up in the code, a potential solution has been identified. Comments have been added to enhance code readability. A single function has been created for the next and previous buttons, and unnecessary looping has been removed. Tests have been conducted on multiple browsers.

Feel free to try out the updated code. If there are any aspects you find confusing, please let me know in the comments. :)

/* JavaScript code enhanced for next and previous buttons */
 /* Example HTML code for modals */ 

Answer №3

Did you attempt to implement this in your CSS stylesheet?

html {
  overflow(x, y): hidden;
}

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

What are the various ways to incorporate material graphic elements on an HTML static webpage?

Currently, I am working on creating a static website that will be stored on CDs and USB sticks. This website serves as a manual for a specific product. I am considering using a graphic framework like Onsen UI, but I have found that I prefer Material UI. ...

Angular is having trouble with the toggle menu button in the Bootstrap template

I recently integrated this template into my Angular project, which you can view at [. I copied the entire template code into my home.component.html file; everything seems to be working fine as the CSS is loading correctly and the layout matches the origina ...

The jQuery UI Tabs Ajax feature seems to only be displaying content in the first tab that is

My jQuery UI tabs are set up to load their content using the ajax method: <div id="tabs"> <ul> <li><a href="/messages/inbox" title="Inbox"><span>Inbox</span></a></li> <li><a href ...

Is there a method to instruct angular-cli (for angular 2) to produce a minified version of css files?

When running "ng serve" in angular-cli, the generated css is not minified as expected. Is there a specific setting to configure in angular-cli-build or an additional plugin to install? This is the content of my angular-cli-build.js file: var Angular2App ...

Passing two variables through a Javascript URL to dynamically update a dropdown menu based on the specified values

What is the best way to send two variables to a JavaScript function in order to modify the dropdown list according to those values? $(document).ready(function() { $("#date").change(function() { $(this).after('<div id="loader">& ...

Tips for aligning the timing of two CSS animations

I'm struggling to achieve a synchronized CSS animation where a progress bar fills up and a background changes simultaneously. For example, I want the progress bar to fill in 5000ms, with the background image changing every 5000ms as well. Despite se ...

Controlling Javascript events

Currently, I am working on implementing an in-place editor using jQuery. The functionality is such that when you click on the text you wish to edit, it will replace the content with an input field, specifically a select tag. Everything seems to be functio ...

Adding up results from Ajax request based on specific criteria

I am performing an Ajax Request that retrieves data from two different databases. For example, I have 2 databases and my ajax request queries those databases to return the following information: [{"question_id":31,"columnheader":"joene_001","ct_yes":"2"," ...

Create a variety of elements in real-time

My goal is to utilize JavaScript in order to generate a specific number of input boxes based on the user's input. However, I encountered an issue where using a for loop only creates one input box and then appends this same input box multiple times. f ...

Ways to enhance numerical count and showcase it on a dynamically inserted div using jQuery

There is a link on top of a static div that is already loading by default (Div number 1). As shown in the image, each time I click on the hyperlink, a new div is added beneath every existing one. I want to be able to increase the count of the div numbers ...

My div remains stationary despite keyframes

I've been attempting to create a div that moves up and down using CSS3, but unfortunately it's not working properly. <div class="globo"></div> @-webkit-keyframes mover { 0%, 100% { top: 0%;} 50% { top: 5%; } } @-moz-keyframe ...

Eliminate the Bootstrap design for ButtonGroup

I'm curious if there is a method to eliminate the default styling and alignment imposed by the <ButtonGroup>. In my project that utilizes React and React Bootstrap's <Row>, <Col>, and <Form.Row>, I have already applied sty ...

How can I use AJAX to read an image file and display it on a Canvas?

Is there a way to read an image from my server using Ajax and then display it on Canvas? I am aware that this can be achieved by using normal image tags and canvas draw methods, as shown below: <img id="myImage" src="./ColorTable.PNG" style="display:n ...

PAYPAL PAYMENT Integration using REST API

I have integrated PayPal Client Side REST to my website. The sample code provided from the link below was used: https://developer.paypal.com/demo/checkout/#/pattern/client The code below was working for over a month, but today it is showing the following ...

Utilizing Jquery in the Customer Portal feature within the Salesforce platform

Struggling to incorporate a Visualforce page with Jquery UI and Salesforce Customer Portal. Unfortunately, Jquery UI seems to be taking precedence over the Standard Salesforce stylesheet. Is there a way to prevent this from happening? Any assistance on t ...

Utilizing CSS3 to perform multiple 3D rotations

I am attempting to rotate a face of a cube twice in order to have that face fall flat on one side. For reference, please see the illustration of my goal here: https://i.stack.imgur.com/kwO8Z.png I have included my current progress below, using a 45-deg ...

Caching a large JSON file on both the server and client side

My task involves generating and delivering a rather large JSON file to users, clocking in at around 400 KB. This file is essentially the combination of data from two tables, and it's likely to grow in size as time goes on. The purpose of this JSON fil ...

"Resolving the Problem of a CSS Width Setting at 100

Encountering difficulties when using 100% in the container CSS. Referencing the attached image, you can see the RED background color displayed on the right side image. Below is the link to my CSS code on jsfiddle. Please review it and advise on the modifi ...

The Sacred Chalice Trio Vertical Stretch Design

Recently, I've been working on creating a three column layout with percentage width. I stumbled upon the concept of the Holy Grail web design through some online articles and resources. To implement this layout, I referred to an example from The Perfe ...

Ways to retrieve information from a different website's URL?

Having a bit of an issue here. I'm currently browsing through some reports on webpage #1 () and I have a specific requirement to extract the object named "data" from webpage #2 (). However, the code I've used seems to fetch the entire webpage ins ...