My webpage includes a div styled with the following CSS:
.modal {
display: none;
position: fixed;
z-index: 1000;
top: 0;
left: 0;
height: 100%;
width: 100%;
background: rgba( 255, 255, 255, .35 )
url('/NoAuth/cf/ajax-loader.gif')
50% 50%
no-repeat;
}
/* Whenever the body has the loading class, our
modal element will be visible */
body.loading .modal {
display: block;
}
To trigger the "loading" class on the body when an .ajax call is made, I use the following jQuery function:
jQuery(document).ready(function () {
jQuery('body').on({
ajaxStart: function() {
jQuery(this).addClass('loading');
},
ajaxStop: function() {
jQuery(this).removeClass('loading');
}
});
});
However, I often encounter a situation where the background color changes during the ajax call, but the gif does not appear. Strangely, if I manually add the 'loading' class using
jQuery('body').addClass('loading')
in the Firebug console, the image displays properly. Looking at the "Net" tab in Firebug reveals that the gif was only loaded after executing the .addClass command.
Is there a way to pre-load the background to ensure it appears consistently, or how can I address and prevent this issue?