Skip to content

Commit 7a88baf

Browse files
committed
support custom width and height
1 parent fe11da4 commit 7a88baf

File tree

2 files changed

+89
-22
lines changed

2 files changed

+89
-22
lines changed

examples/index.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,68 @@
22

33
---
44

5-
## Normal usage
5+
## 一般使用
66

77
````html
8-
<div style="border:1px dashed #ececec;width:200px;height:300px;" id="loader-box"></div>
8+
<div style="border:1px dashed #ececec;width:200px;height:100px;" id="loader-box"></div>
99
````
1010

1111
````javascript
1212
seajs.use('index', function(loader) {
1313
new loader({
14-
target:'#loader-box',
14+
target:'#loader-box'
15+
}).show();
16+
});
17+
````
18+
19+
## 个数设定
20+
21+
````html
22+
<div style="border:1px dashed #ececec;width:200px;height:100px;" id="loader-box-2"></div>
23+
````
24+
25+
````javascript
26+
seajs.use('index', function(loader) {
27+
new loader({
28+
target:'#loader-box-2',
1529
number:8
1630
}).show();
1731
});
1832
````
33+
34+
## 宽度设定
35+
36+
````html
37+
<div style="border:1px dashed #ececec;width:200px;height:100px;" id="loader-box-3"></div>
38+
````
39+
40+
````javascript
41+
seajs.use('index', function(loader) {
42+
new loader({
43+
target:'#loader-box-3',
44+
number:6,
45+
style:{
46+
width:'20px'
47+
}
48+
}).show();
49+
});
50+
````
51+
52+
## 高度设定
53+
54+
````html
55+
<div style="border:1px dashed #ececec;width:200px;height:100px;" id="loader-box-4"></div>
56+
````
57+
58+
````javascript
59+
seajs.use('index', function(loader) {
60+
new loader({
61+
target:'#loader-box-4',
62+
number:6,
63+
style:{
64+
width:'20px',
65+
height:'10px'
66+
}
67+
}).show();
68+
});
69+
````

index.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,48 @@ var $ = require('jquery'),
33

44
function loader(option) {
55
require('./loader.css');
6-
var _this = this,
7-
o = {
8-
number: 4,//bars numbers
9-
speed: 100,
10-
style: {
11-
width: '8px',
12-
height: '4px',
13-
backgroundColor: '#CCC',
14-
activeBackgroundColor: '#EA578C'
15-
}
16-
};
17-
$.extend(o, option);
18-
var barsHtml = new Array(o.number).join('<i></i>');
6+
var _this = this;
7+
var defaults = {
8+
number: 4,//bars numbers
9+
speed: 100,
10+
style: {
11+
width: '8px',
12+
height: '4px',
13+
backgroundColor: '#CCC',
14+
activeBackgroundColor: '#EA578C'
15+
}
16+
};
17+
_this.o = {};
18+
$.extend(_this.o, defaults, option);
19+
20+
// custom style
21+
var style = [];
22+
for (var i in _this.o.style) {
23+
if (this.o.style[i] !== defaults.style[i]) {
24+
style.push(i + ':' + this.o.style[i]);
25+
}
26+
}
27+
style = style.join(';').replace('backgroundColor', 'background-color');
28+
var barsHtml = new Array(_this.o.number).join('<i style="' + style + '"></i>');
29+
1930
this.overlay = new Overlay({
20-
template: '<span class="mk-loading"><i class="mk-loading-active"></i>' + barsHtml + '</span>',
21-
align: o.align,
31+
template: '<span class="mk-loading"><i class="mk-loading-active" style="' + style + '"></i>' + barsHtml + '</span>',
2232
align: {
2333
selfXY: ['50%', '50%'],
2434
baseElement: option.target,
2535
baseXY: ['50%', '50%']
2636
}
2737
});
38+
this.play();
39+
return this;
40+
}
2841

42+
loader.prototype.play = function () {
2943
var $loading = this.overlay.element,
3044
$items = $loading.find('i'),
31-
length = $items.length;
32-
var playInterval = setInterval(function () {
45+
length = $items.length,
46+
_this = this;
47+
setInterval(function () {
3348
var $active = $loading.find('.mk-loading-active'),
3449
thisone = $active.index() + 1,
3550
lastone = thisone - 1;
@@ -38,8 +53,9 @@ function loader(option) {
3853
lastone = length - 1;
3954
}
4055
$items.eq(lastone).removeClass('mk-loading-active').end().eq(thisone).addClass('mk-loading-active');
41-
}, o.speed);
42-
}
56+
}, _this.o.speed);
57+
return this;
58+
};
4359

4460
loader.prototype.show = function () {
4561
this.overlay.show();

0 commit comments

Comments
 (0)