Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to grey out bootstrap modal when a spinner is showing?

I the example below, use can open a modal, type a message and then send it... It would take 2 seconds to send the message... I would like to show a spinner (which is already working) but also I want to background of the modal to be greyed-out while spinner is showing...

$("#sendBtn").click(function() {
  $('.sending-message-spinner').show();
      setTimeout(function () {
        $("#myModal").modal("hide");
    }, 2000);
});
textarea {
   width: 100%;
   height: 80px;
}

@keyframes loading {
   0% {
       transform: rotate(0);
   }
   100% {
      transform: rotate(360deg);
   }
}

.sending-message-spinner {
   position: fixed;
   text-align: center;
   z-index: 20;
   display: none;
   width: 40px;
   height: 40px;
   border: 8px solid #B0B0B0;
   border-radius: 50%;
   border-top-color: #000;
   animation: loading .75s linear infinite;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open modal
  </button>
  <div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <div class="modal-body">
          <div class="d-flex justify-content-center">
              <div class="feedback-position sending-message-spinner">
              </div>
            </div>
            <p>type your message: </p>
            <Textarea> </textarea>
            <button id='sendBtn' type="button" class="btn btn-primary">Send Message</button>
        </div>
      </div>
    </div>
  </div>
  
</div>

</body>
</html>
like image 592
Hooman Bahreini Avatar asked Oct 21 '25 23:10

Hooman Bahreini


1 Answers

You can add a class to CSS and have your JS call it when the button is clicked. I added to CSS .myClass and set the opacity in there, then called it in JS with $("#myModal").addClass('myClass'); to add that class to myModal when the button is clicked. See here:

UPDATE: Here I did it with an overlay div inside myModal This will grey out the modal without making it transparent.

$("#sendBtn").click(function() {
  $("#overlay").show();
  $('.sending-message-spinner').show();
      setTimeout(function () {
        $("#myModal").modal("hide");
    }, 2000);
});
textarea {
  width: 100%;
  height: 80px;
}

@keyframes loading {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}

.sending-message-spinner {
  position: fixed;
  text-align: center;
  z-index: 20;
  display: none;
  width: 64px;
  height: 64px;
  border: 8px solid #B0B0B0;
  border-radius: 50%;
  border-top-color: #000;
  animation: loading .75s linear infinite;
}

#overlay {
  position: fixed;
  display: none;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0,0,0,0.5);
  z-index: 10;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open modal
  </button>
  
  <div class="modal" id="myModal">
  <div id="overlay"></div>
    <div class="modal-dialog">
      <div class="modal-content">
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <div class="modal-body">
          <div class="d-flex justify-content-center">
              <div class="feedback-position sending-message-spinner">
              </div>
            </div>
            <p>type your message: </p>
            <Textarea> </Textarea>
            <button id='sendBtn' type="button" class="btn btn-primary">Send Message</button>
        </div>
      </div>
    </div>
  </div>
  
</div>

</body>
</html>
like image 172
John Avatar answered Oct 24 '25 15:10

John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!