How To Show Loading Spinner In Ajax jQuery

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>How To Show Loading Spinner In Ajax jQuery - Websolutionstuff</title>
<style>
.overlay{
display: none;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 999;
background: rgba(255,255,255,0.8) url("loader.gif") center no-repeat;
}
body.loading{
overflow: hidden;
}
body.loading .overlay{
display: block;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).on("click", "button", function(){
$.get("customers.php", function(data){
$("body").html(data);
});
});
$(document).on({
ajaxStart: function(){
$("body").addClass("loading");
},
ajaxStop: function(){
$("body").removeClass("loading");
}
});
</script>
</head>
<body style="text-align: center;">
<button type="button">Click Here...</button>
<p>Click the above button to get the customers details from the web server via Ajax.</p>
<div class="overlay"></div>
</body>
</html>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Websolutionstuff

Websolutionstuff

96 Followers

I am Laravel and PHP Developer. I have also Good Knowledge of JavaScript, jQuery, Bootstrap and REST API.