NProgress.js加载进度插件的简单实用方法

NProgress.js

  说明:

    NProgress是基于jquery的,且版本要 >1.8

  下载地址:

    https://github.com/rstacruz/nprogress

  API:

    NProgress.start() — 启动进度条

 

    NProgress.set(0.4) — 将进度设置到具体的百分比位置

 

    NProgress.inc() — 少量增加进度

 

    NProgress.done() — 将进度条标为完成状态

  使用步骤:

    1. 引入

1 <link rel="stylesheet" type="text/css" href="css/NProgress.css">
2 <script src="js/NProgress.js" type="text/javascript"></script><br>//还有jquery要之前引入进来  

    2. 使用场景一:页面加载的效果  开始页面加载开始进度条  页面加载好  结束进度条:

<script>
$(function() {
    NProgress.start();
    $(window).load(function() {
        NProgress.done();
  });
</script>

    3.使用场景二:ajax发送的效果  开始发送开始进度条  成功后  结束进度条:

<script>
$(window).ajaxStart(function () {
    NProgress.start();
});
$(window).ajaxStop(function () {
   NProgress.done();
});
</script>
//技巧总结 此方法挂在window上 是为了监听项目中所有的ajax请求
//ajax事件 可参考 jquery 文档 http://jquery.cuishifeng.cn/

 

posted @ 2018-01-30 19:43  Sugеr  阅读(343)  评论(0)    收藏  举报