luv2code
luv2code

Reputation: 1296

Adding a page loader using jQuery

I need a little help. I do not know where to start. I need to add a page loader to my website. I first submit a form it then uses SimpleXML to call an exterior XML Sheet with expedia... It takes a minute to load, so I would like to add the image loader to this page. But how do I go about doing this? I have looked on Google and could not find any useful info.

I need it to show the loader until the COMPLETE page has loaded.

Upvotes: 4

Views: 76994

Answers (3)

Nirendra Singh Panwar
Nirendra Singh Panwar

Reputation: 61

This loader div will show a loading image on page load.

$(window).load(function() {
  $(".pageloader").fadeOut("slow");
});
body {
  background: black;/* for this demo */
}

.pageloader {
  position: fixed;
  left: 0px;
  top: 0px;
  width: 100%;
  height: 100%;
  z-index: 9999;
  background: url('images/yourLoaderImage.gif') 50% 50% no-repeat rgb(249, 249, 249);
  opacity: .8;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="pageloader"></div>

It's Completely working for me.

Upvotes: 2

Connor
Connor

Reputation: 1034

Check out spin.js http://fgnass.github.com/spin.js/

var opts = {
lines: 13, // The number of lines to draw
length: 7, // The length of each line
width: 4, // The line thickness
radius: 10, // The radius of the inner circle
// Even more options available.... 
};
var target = document.getElementById('loading');
var spinner = new Spinner(opts).spin(target);

And once your form is done:

$("#loading").data('spinner').stop();

Upvotes: 4

Ricardo Souza
Ricardo Souza

Reputation: 16446

This has many solutions, but a simple one is to:

1- Create an overlay DIV with your loader stuff and prepend to BODY;
2- Add an event listener for window.load or document.ready event that hides this DIV.

// On the first line inside BODY tag
<script type="text/javascript">
    jQuery("body").prepend('<div id="preloader">Loading...</div>');
    jQuery(document).ready(function() {
        jQuery("#preloader").remove();
    });
</script>

(code typos fixed)

Upvotes: 17

Related Questions