Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NProgress.js to show progress of page loading

I am trying to figure out how to use NProgress.js as a generic page load for all pages on a site. I am unable to find any documentation or an easy way to add this loading effect on page load and have it finish when the entire page has loaded.

http://ricostacruz.com/nprogress/

Any help is greatly appreciated!

like image 935
Aaron Avatar asked Sep 11 '13 16:09

Aaron


2 Answers

Simply put, place this JavaScript anywhere in your html, it's a good practice to place it before the closing body tag:

<script>

    // Show the progress bar 
    NProgress.start();

    // Increase randomly
    var interval = setInterval(function() { NProgress.inc(); }, 1000);        

    // Trigger finish when page fully loaded
    jQuery(window).load(function () {
        clearInterval(interval);
        NProgress.done();
    });

    // Trigger bar when exiting the page
    jQuery(window).unload(function () {
        NProgress.start();
    });

</script>

Oh and don't be confused, NProgress is in the global scope, it has nothing to do with jQuery, I'm using jQuery's .load()/.unload() for convenience only, and please don't put the NProgress.start() inside a jquery document's .ready(), it's useless clutter.

like image 173
Nabil Kadimi Avatar answered Sep 26 '22 10:09

Nabil Kadimi


In the main page: http://ricostacruz.com/nprogress/

I found this (with show page source code):

<script>
$('body').show();
$('.version').text(NProgress.version);
NProgress.start();
setTimeout(function() { NProgress.done(); $('.fade').removeClass('out'); }, 1000);

$("#b-0").click(function() { NProgress.start(); });
$("#b-40").click(function() { NProgress.set(0.4); });
$("#b-inc").click(function() { NProgress.inc(); });
$("#b-100").click(function() { NProgress.done(); });
</script>

As you can see, you need to use NProgress.start(); to starts de progress bar. Try something like this (i used jquery ready function):

<script>
NProgress.start();
NProgress.set(0.4);
//Increment 
var interval = setInterval(function() { NProgress.inc(); }, 1000);
$(document).ready(function(){
    NProgress.done();
    clearInterval(interval);
});
</script>
like image 28
rodrigoio Avatar answered Sep 23 '22 10:09

rodrigoio