Skip to content

Commit d56978f

Browse files
committed
Implement .inc().
1 parent f4b95cc commit d56978f

3 files changed

Lines changed: 44 additions & 21 deletions

File tree

index.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
<link href='nprogress.css' rel='stylesheet' />
88
</head>
99
<body>
10-
<button id='b0'>0%</button>
11-
<button id='b1'>40%</button>
12-
<button id='b2'>100%</button>
10+
<button id='b-0'>0%</button>
11+
<button id='b-40'>40%</button>
12+
<button id='b-100'>100%</button>
13+
<button id='b-inc'>+</button>
1314

1415
<script src='vendor/jquery-1.10.js'></script>
1516
<script src='nprogress.js'></script>
1617

1718
<script>
18-
$("#b0").click(function() { NProgress.set(0.0); });
19-
$("#b1").click(function() { NProgress.set(0.4); });
20-
$("#b2").click(function() { NProgress.set(1); });
19+
$("#b-0").click(function() { NProgress.set(0.0); });
20+
$("#b-40").click(function() { NProgress.set(0.4); });
21+
$("#b-inc").click(function() { NProgress.inc(); });
22+
$("#b-100").click(function() { NProgress.set(1); });
2123
</script>
2224
</body>
2325
</html>

nprogress.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#nprogress .bar {
77
display: block;
88
background: #29d;
9-
position: absolute;
9+
position: fixed;
1010
z-index: 100;
1111
top: 0;
1212
left: 0;

nprogress.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
var NProgress = {};
77

88
var Settings = NProgress.settings = {
9-
minimum: 0.1,
10-
exitmode: 'full',
9+
minimum: 0.05,
1110
easing: 'ease',
12-
speed: 300,
11+
speed: 200,
1312
template: '<div class="bar" role="bar"><div class="peg"></div></div><div class="spinner"><div></div></div>'
1413
};
1514

@@ -24,6 +23,10 @@
2423
$.extend(Settings, options);
2524
};
2625

26+
/**
27+
* Last number.
28+
*/
29+
2730
NProgress.status = null;
2831

2932
/**
@@ -35,7 +38,7 @@
3538

3639
NProgress.set = function(n) {
3740
n = clamp(n, Settings.minimum, 1);
38-
NProgress.status = n;
41+
NProgress.status = (n === 1 ? null : n);
3942

4043
var $progress = NProgress.render(),
4144
$bar = $progress.find('[role~="bar"]'),
@@ -70,8 +73,20 @@
7073
}, 0);
7174
};
7275

76+
/**
77+
* Shows the progress bar.
78+
* This is the same as setting the status to 0%.
79+
*
80+
* NProgress.show();
81+
*
82+
*/
83+
NProgress.show = function() {
84+
return NProgress.set(0);
85+
};
86+
7387
/**
7488
* Hides the progress bar.
89+
* This is the same as setting the status to 100%.
7590
*
7691
* NProgress.hide();
7792
*/
@@ -80,6 +95,22 @@
8095
return NProgress.set(1);
8196
};
8297

98+
/**
99+
* Increments by a random amount.
100+
*/
101+
102+
NProgress.inc = function() {
103+
var n = NProgress.status;
104+
105+
if (!n) {
106+
return NProgress.show();
107+
} else {
108+
n += (1 - n) * clamp(Math.random() * n, 0.1, 0.95);
109+
n = clamp(n, 0, 0.994);
110+
return NProgress.set(n);
111+
}
112+
};
113+
83114
/**
84115
* (Internal) renders the progress bar markup based on the `template`
85116
* setting.
@@ -97,8 +128,6 @@
97128
transform: 'translate3d(-100%,0,0)'
98129
});
99130

100-
$el[0].innerWidth;
101-
102131
return $el;
103132
};
104133

@@ -110,14 +139,6 @@
110139
return ($("#nprogress").length > 0);
111140
};
112141

113-
/**
114-
* Shows the progress bar.
115-
*
116-
*/
117-
NProgress.show = function() {
118-
return NProgress.set(NProgress.settings.minimum);
119-
};
120-
121142
/**
122143
* Helpers
123144
*/

0 commit comments

Comments
 (0)