In the following example, you can open a modal, enter a message, and send it. The message will take 2 seconds to send. I have implemented a spinner to show while the message is sending, but I also want the background of the modal to be greyed-out during that time.
$("#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">×</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>