Skip to content

Instantly share code, notes, and snippets.

@maxparm
Last active January 21, 2021 02:57
Show Gist options
  • Save maxparm/4513078 to your computer and use it in GitHub Desktop.
Save maxparm/4513078 to your computer and use it in GitHub Desktop.
Javascript - Toggling a loading screen with jQuery ajax
.loading-container {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
display:none;
}
.loading-container:before {
position:absolute;
top:50%;
left:50%;
display:block;
width:100px;
height:100px;
line-height:100px;
margin:-50px 0 0 -50px;
color:#fff;
text-align:center;
background:rgba(0,0,0,.9);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
content: 'loading...';
}
<div id="loading-container" class="loading-container"></div>
<div id="content-container" class="content-container"></div>
$(function () {
// Toggle loading container when ajax call
var loadingContainer = $('#loading-container', this.el);
loadingContainer.ajaxStart(function () {
loadingContainer.show();
});
loadingContainer.ajaxStop(function () {
loadingContainer.hide();
});
// Ajax call
$.getJSON('https://api.github.com/users/maxparm', function (response) {
$('#content-container').html('Response ajax call: ' + response.name);
});
});
@greenspray9
Copy link

Would be cool to have a demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment