Home > Web Front-end > JS Tutorial > How to Ensure Multiple Modals Overlay Correctly with a Visible Backdrop in Bootstrap?

How to Ensure Multiple Modals Overlay Correctly with a Visible Backdrop in Bootstrap?

Patricia Arquette
Release: 2024-10-25 08:26:29
Original
316 people have browsed it

How to Ensure Multiple Modals Overlay Correctly with a Visible Backdrop in Bootstrap?

Multiple Modals Overlaying

In this scenario, you desire the overlay to appear above the first modal, rather than behind it.

Solution

Inspired by contributions from @YermoLamers and @Ketwaroo, a comprehensive solution involves:

Backdrop Z-Index Fix

To address the issue of the backdrop appearing behind the first modal, implement the following JavaScript:

<code class="javascript">$(document).on('show.bs.modal', '.modal', function() {
  const zIndex = 1040 + 10 * $('.modal:visible').length;
  $(this).css('z-index', zIndex);
  setTimeout(() => $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack'));
});</code>
Copy after login

Z-Index Customization

If you wish to customize the z-index calculation, you can employ the following:

<code class="javascript">const zIndex = 10 +
  Math.max(...Array.from(document.querySelectorAll('*')).map((el) => +el.style.zIndex));</code>
Copy after login

Scrollbar Fix

To resolve cases where closing a second modal prevents scrolling in a modal that exceeds the browser height, include the following JavaScript:

<code class="javascript">$(document).on('hidden.bs.modal', '.modal',
  () => $('.modal:visible').length && $(document.body).addClass('modal-open'));</code>
Copy after login

Versions

This solution is thoroughly tested with Bootstrap versions 3.1.0 through 3.3.5. By implementing this solution, you can ensure that multiple modals overlay correctly, with the backdrop appearing on top of the first modal and the scrollbar remaining functional as needed.

The above is the detailed content of How to Ensure Multiple Modals Overlay Correctly with a Visible Backdrop in Bootstrap?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template