-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.progress.js
94 lines (84 loc) · 2.35 KB
/
jquery.progress.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* @Author: [email protected]
* @Date: 2018-12-13 14:45:54
* @Last Modified by: CoderQiQin
* @Last Modified time: 2018-12-13 14:48:11
*/
if (typeof jQuery === 'undefined') {
throw new Error('请先引用jquery')
}
(function($){
function Progress(el, options) {
this.el = el
this.options = options
this._init()
}
Progress.prototype._init = function() {
var docHeight = $('body').height() // 文档高度
var sercHeight = $(window).height() // 屏幕高度
var resHeight = docHeight - sercHeight
var config = this.options
var proConfig = {
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '2px',
backgroundColor: '#eee',
zIndex: 9999
}
var innerConfig = {
width: 0,
backgroundColor: '#50bcb6',
height: '100%',
transitionProperty: 'width',
transitionDuration: '.3s',
transitionTimingFunction: 'linear'
}
var proMap = ['size', 'position', 'wapperBg', ]
var innerMap = ['innerBg', 'duration', 'effect']
var setMap = proMap.concat(innerMap)
for(let key in config) {
if(!setMap.includes(key)) delete config[key]
switch (key) {
case 'size':
proConfig.height = config.size
break;
case 'position':
if(config.position === 'bottom') {
delete proConfig.top
proConfig['bottom'] = 0
}
break;
case 'wapperBg':
proConfig.backgroundColor = config.wapperBg
break;
case 'innerBg':
innerConfig.backgroundColor = config.innerBg
break;
case 'duration':
innerConfig.transitionDuration = config.duration
break;
case 'effect':
innerConfig.transitionTimingFunction = config.effect
break;
default:
break;
}
}
this.el.empty().css(proConfig)
$('<div class="inner"></div>').appendTo(this.el).css(innerConfig)
$(window).scroll(function(e) {
window.requestAnimationFrame(function(){
var width = Math.max(0, Math.min(1, $(window).scrollTop() / resHeight))
$('.inner').show().css('width', width * 100 +'%')
})
})
}
$.fn.progress = function(options) {
this.each(function(el) {
new Progress($(this), options || null)
})
return this
}
})(jQuery)