Close Load when opening modal

Asked

Viewed 256 times

1

How do I stop the one gif, when my modal opens? When I click on the icon FA-SEARCH carries my modal, and the gif becomes infinite.

<script type="text/javascript">
    $(document).ready(function () {

            $('.loading').hide();

        $('.fa-search').click(
                function () {

                    $('.loading').show();
                }
            );
        }
    );
</script>
.loading {
    background: url('https://i.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.webp') no-repeat center center;
    background-color: #114876bf;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    z-index: 9999999;
}
}
<div class="loading"></div>
<a style="margin-left:-22px;" href="#" id="addCliente">
                                        <i class="fa fa-search"></i>
                                    </a>

$("#addCliente").click(function () {
        var id = $(this).attr("data-id");
        //alert(id);
        var url = '@Url.Action("adicionarCliente", "Atendimento", new { area = "Suporte" })';
        $("#addClientem").load(url,
            function() {
                $("#addClientem").modal();
            });
    });
  • your modal is bootstrap?

  • it is yes, I edited, I forgot to put a script

2 answers

2

bootstrap launches an event where you set up a callback to run after the modal is displayed. Example:

$('#addClientem').on('shown.bs.modal', function () {
  //seu código aqui para pós abertura
})

If you need to also monitor the modal closing change shown.bs.modal for hidden.bs.modal. Example:

$('#addClientem').on('hidden.bs.modal', function () {
  //seu código aqui para pós-fechamento
})
  • The best and most consistent response. Congratulations!

0


You can use the method setTimeout to give Hide in loading and show in modal:

$(() => {
  $('.loading').hide();

  $('.fa-search').on('click', () => {
      $('.loading').show();
      setTimeout(() => {
        $('.loading').hide();
        $('.modal').modal();
      }, 1000)
    }
  );
});
.loading {
  background: url('https://i.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.webp') no-repeat center center;
  background-color: #114876bf;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 9999999;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<div class="loading"></div>
<i class="fa fa-search">Ícone</i>

<div class="modal fade" id="modalExemplo" 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">Título do modal</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
          <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">Salvar mudanças</button>
      </div>
    </div>
  </div>
</div>

  • this is super simple, and easy to implement!! obg

  • 1

    Nice that helped man.

  • 1

    Sorry, but using setTimeout for this is the worst solution, bordering on the gambiarra. The Bootstrap modal has a callback for this, as reported by the other answer.

  • Ahan, I gave an example and there’s the other example, so it’s up to him to choose what he’s going to implement. I don’t think I’ll go for it if the method allows you to do that!

Browser other questions tagged

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