How to put a small modal bootstrap on another larger modal with the fade working properly on the two backgrounds?

Asked

Viewed 31 times

0

inserir a descrição da imagem aquiI have two modal bootstrap , the first one a modal-Xl, the second one a modal-Sm that is smaller , they normally open. But the first of the effect of fade straight, already when I open the second the lower modal that is larger does not give the effect fade.

With this, the user may get confused, because the two modals are with equal visibility. I need that when opening the second modal, the first is faded.

  • Guy puts a minimum of code there so that at least we simulate his problem

1 answer

2


My dear, you may be solving your problem this way, in bootstrap, by including two functions that make the modification in modal css, in different events:

 function esconderModalFundo(){
      $('#exampleModal').css('z-index', '0');
    }

    function mostrarModalFundo(){
      $('#exampleModal').css('z-index', '1050');
    }
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Teste de modal</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
  
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Abrir modal
</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg p-5" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal2" onclick="esconderModalFundo()">Abrir segunda modal</button>
      </div>
    </div>
  </div>
</div>


<!-- Modal -->
<div class="modal fade xl-12" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Segunda Modal </h5>
        <button type="button" class="close" onclick="mostrarModalFundo()" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal" onclick="mostrarModalFundo()">Fechar</button>
        <button type="button" class="btn btn-primary">Salvar</button>
      </div>
    </div>
  </div>
</div>



  
  
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

  • Solved my problem perfectly, very thankful.

  • 1

    So that the z-index of the first modal fits when the user closes the second by clicking only outside it and not in the 'X' you can use the code below $('#exampleModal2'). on("Hidden.bs.modal", Function(e) { $('#exampleModal'). css('z-index', '1050'); })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.