-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.readingbar.js
92 lines (87 loc) · 2.64 KB
/
jquery.readingbar.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
/*!
* Readingbar jQuery plugin
*
* @author : http://twitter.com/nicovanzyl
* @author : http://twitter.com/wixelhq
* @url : https://github.com/Wixel/readingbar.git
* @copyright: 2016 Wixel
* @license : MIT license
* @version : 1.1
*/
(function($) {
$.fn.viewportOffset = function() {
var offset = $(this).offset();
return {
top: offset.top + $(window).scrollTop()
};
};
$.fn.readingbar = function(options) {
if (($('.read-bar').length > 0) === false) {
var defaults = {
backgroundColor: '#50E3C2',
height: 4,
counter: true
};
settings = $.extend({}, defaults, options);
$('<div class="read-bar"></div>').appendTo('body');
$('.read-bar').css({
position: 'fixed',
bottom: '0',
left: '0',
width: '0',
maxWidth: '100%',
});
$('.read-bar').css({
height: settings.height + 'px',
backgroundColor: settings.backgroundColor
});
if(settings.counter){
$('<span class="read-text"></span>').appendTo('body');
$('.read-text').css({
position: 'fixed',
bottom: settings.height + 'px',
left: '0',
width: settings.height*10 + 'px',
marginLeft: '-' + settings.height*10 + 'px',
textAlign: 'right',
color: settings.backgroundColor,
fontSize: settings.height*5 + 'px'
});
}
}
_ = $(this);
var readHeight = _.outerHeight();
var startPoint = _.offset().top * 1.65;
var currentPos = 0;
$(document).on('scroll', function(){
readHeight = _.outerHeight();
currentPos = (_.viewportOffset().top - startPoint) / readHeight * 100;
$('.read-bar').css('width', currentPos + '%');
$('.read-text').css('left', currentPos + '%');
if (currentPos > 100) {
$('.read-bar, .read-text').css('opacity', '0');
}else{
$('.read-text').text(Math.round(currentPos) + '%');
$('.read-text').css({
opacity: 1,
bottom: '0'
});
$('.read-bar').css({
opacity: 1,
height: settings.height + 'px'
});
clearTimeout($.data(this, 'scrollTimer'));
$.data(this, 'scrollTimer', setTimeout(function() {
$('.read-bar').animate({
opacity: 0.8,
height: settings.height/2 + 'px'
}, 150);
$('.read-text').animate({
opacity: 0,
bottom: '-' + settings.height*10 + 'px'
}, 140);
}, 250));
}
});
};
}(jQuery));