Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacking modals scrolls the main page when one is closed [duplicate]

With Bootstrap 3.3.1, I was able to get stacking modals without any additional scripts (as in copy-pasting the modal templates in the Bootstrap site and making many of them in the same page). The problem was that whenever the top (highest) modal is closed, scrolling focus goes to the main page (under all remaining modals) and goes back to the top modal only if a new modal is opened. Is there a way to set the scrolling focus to the next modal and not the main page?

When I tested the solution here (and even this one) by adding text to the body and individual modals (through Firefox's "Edit as HTML" to test scrolling) and it had the characteristics that I needed.

A problem that occurred when I tried it with the latest jQuery and Bootstrap was that the modal-backdrop then shows above the modal-dialog. Upon Inspect Element whenever one or more modals are up, I noticed that the div for the modal-backdrop appears within the main div of the modal:

<div id="myModal" class="modal fade" aria-hidden="true" style="display: none;">
  <div class="modal-backdrop fade in"></div>
  <div class="modal-dialog modal-lg"></div>
</div>

as compared to the bottom of body like it is here. This even happens when I use 3.3.1 on that linked example. I think this resulted in the main div and modal-backdrop having the modified z-index, but not the modal-dialog, so I tried to fix it by adding a line to set the modal-dialog to have its parent's z-index plus 1. It put the backdrops in their proper place, but the scrolling problem persists. Is this because of a change in 3.3.1 or was I looking at the wrong solution?

like image 924
maki57 Avatar asked Dec 09 '14 05:12

maki57


People also ask

Should modals be scrollable?

The goal is to keep context, therefore a modal should not take up the whole screen view. Content should fit the modal. If a scrollbar is needed, you may consider creating a new page instead.

How do I keep my modal from scrolling open?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.

Can you have two modals at once?

A double modal is a linguistic construction in which two modal auxiliaries are used in succession to introduce two separate modalities into a sentence. For example, to express that there is a possibility that you have the ability to jump over the bush, you might say something like, “I might can jump over that bush”.


1 Answers

Backdrop's not the problem. Not being able to scroll through shown modals if the top one is closed is.

You should add the following Javascript:

$('.modal').on('hidden.bs.modal', function (e) {
    // For Bootstrap 4:
    // if ($('.modal').hasClass('show')) {
    if ($('.modal').hasClass('in')) {
        $('body').addClass('modal-open');
    }    
});

demo: http://jsfiddle.net/u038t9L0/1/

When the body has class modal-open the following CSS will be applied for your .modals:

.modal-open .modal {
  overflow-x: hidden;
  overflow-y: auto;
}

Closing the modal will remove the modal-open class from the modal too.

like image 144
Bass Jobsen Avatar answered Oct 19 '22 20:10

Bass Jobsen