-
Notifications
You must be signed in to change notification settings - Fork 160
/
simple-scrollbar.js
165 lines (129 loc) · 4.6 KB
/
simple-scrollbar.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
;(function(root, factory) {
if (typeof exports === 'object') {
module.exports = factory(window, document)
} else {
root.SimpleScrollbar = factory(window, document)
}
})(this, function(w, d) {
var raf = w.requestAnimationFrame || w.setImmediate || function(c) { return setTimeout(c, 0); };
function initEl(el) {
Object.defineProperty(el, 'data-simple-scrollbar', { value: new SimpleScrollbar(el), configurable: true });
}
function unbindEl(el) {
if (!Object.prototype.hasOwnProperty.call(el, 'data-simple-scrollbar')) return;
el['data-simple-scrollbar'].unBind();
//Remove the elements property
delete el['data-simple-scrollbar'];
}
// Mouse drag handler
function dragDealer(el, context) {
var lastPageY;
el.addEventListener('mousedown', function(e) {
lastPageY = e.pageY;
el.classList.add('ss-grabbed');
d.body.classList.add('ss-grabbed');
d.addEventListener('mousemove', drag);
d.addEventListener('mouseup', stop);
return false;
});
function drag(e) {
var delta = e.pageY - lastPageY;
lastPageY = e.pageY;
raf(function() {
context.el.scrollTop += delta / context.scrollRatio;
});
}
function stop() {
el.classList.remove('ss-grabbed');
d.body.classList.remove('ss-grabbed');
d.removeEventListener('mousemove', drag);
d.removeEventListener('mouseup', stop);
}
}
// Constructor
function ss(el) {
this.target = el;
this.content = el.firstElementChild;
this.direction = w.getComputedStyle(this.target).direction;
this.bar = '<div class="ss-scroll">';
//Create a reference to the function binding to remove the event listeners
this.mB = this.moveBar.bind(this);
this.wrapper = d.createElement('div');
this.wrapper.setAttribute('class', 'ss-wrapper');
this.el = d.createElement('div');
this.el.setAttribute('class', 'ss-content');
if (this.direction === 'rtl') {
this.el.classList.add('rtl');
}
this.wrapper.appendChild(this.el);
while (this.target.firstChild) {
this.el.appendChild(this.target.firstChild);
}
this.target.appendChild(this.wrapper);
this.target.insertAdjacentHTML('beforeend', this.bar);
this.bar = this.target.lastChild;
dragDealer(this.bar, this);
this.moveBar();
w.addEventListener('resize', this.mB);
this.el.addEventListener('scroll', this.mB);
this.el.addEventListener('mouseenter', this.mB);
this.target.classList.add('ss-container');
var css = w.getComputedStyle(el);
if (css['height'] === '0px' && css['max-height'] !== '0px') {
el.style.height = css['max-height'];
}
this.unBind = function() {
//Remove event listeners
w.removeEventListener('resize', this.mB);
this.el.removeEventListener('scroll', this.mB);
this.el.removeEventListener('mouseenter', this.mB);
this.target.classList.remove('ss-container');
//Unwrap the initial content and remove remaining wrappers
this.target.insertBefore(this.content, this.wrapper);
this.target.removeChild(this.wrapper);
//Remove the bar including its drag-dealer event listener
this.target.removeChild(this.bar);
this.bar = null; //make way for the garbage collector
}
}
ss.prototype = {
moveBar: function(e) {
var totalHeight = this.el.scrollHeight,
ownHeight = this.el.clientHeight,
_this = this;
this.scrollRatio = ownHeight / totalHeight;
var isRtl = _this.direction === 'rtl';
var right = isRtl ?
(_this.target.clientWidth - _this.bar.clientWidth + 18) :
(_this.target.clientWidth - _this.bar.clientWidth) * -1;
raf(function() {
// Hide scrollbar if no scrolling is possible
if(_this.scrollRatio >= 1) {
_this.bar.classList.add('ss-hidden')
} else {
_this.bar.classList.remove('ss-hidden')
_this.bar.style.cssText = 'height:' + Math.max(_this.scrollRatio * 100, 10) + '%; top:' + (_this.el.scrollTop / totalHeight ) * 100 + '%;right:' + right + 'px;';
}
});
}
}
function initAll() {
var nodes = d.querySelectorAll('*[ss-container]');
for (var i = 0; i < nodes.length; i++) {
initEl(nodes[i]);
}
}
function unbindAll() {
var nodes = d.querySelectorAll('.ss-container');
for (var i = 0; i < nodes.length; i++) {
unbindEl(nodes[i]);
}
}
d.addEventListener('DOMContentLoaded', initAll);
ss.initEl = initEl;
ss.initAll = initAll;
ss.unbindEl = unbindEl;
ss.unbindAll = unbindAll;
var SimpleScrollbar = ss;
return SimpleScrollbar;
});