-
Notifications
You must be signed in to change notification settings - Fork 33
/
fakescroll.js
203 lines (161 loc) · 7.89 KB
/
fakescroll.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* By Yair Even Or (C)
* https://github.com/yairEO/fakescroll
*/
;(function(){
raf = window.requestAnimationFrame || function(cb) { return window.setTimeout(cb, 1000 / 60) };
function FakeScroll(targetElm, settings){
if( !targetElm ) return;
this.settings = Object.assign({}, this.defaults, settings || {});
this.callback = settings.callback ? settings.callback : null;
this.state = {};
this.listeners = {};
this.DOM = this.build(targetElm);
this.events.binding.call(this, this.DOM);
// run "moveBar" once
setTimeout(this.moveBar.bind(this));
}
FakeScroll.prototype = {
defaults : {
classname : "",
track : false // "smooth" will enable smooth scroll
},
/**
* Build the DOM needed
*/
build( targetElm ){
var DOM = {};
scopeHTML = `<div class="fakeScroll__wrap">
<div class="fakeScroll__content"></div>
</div>
<div class='fakeScroll__track ${this.settings.classname}'>
<div class="fakeScroll__bar"></div>
</div>`,
fragment = document.createDocumentFragment();
// move all the children of the target element into a fragment
while( targetElm.childNodes.length ){
fragment.appendChild(targetElm.childNodes[0]);
}
targetElm.insertAdjacentHTML('afterbegin', scopeHTML);
DOM.scope = targetElm;
DOM.scrollWrap = targetElm.firstElementChild;
DOM.scrollContent = DOM.scrollWrap.firstElementChild;
DOM.scrollContent.appendChild(fragment);
DOM.track = DOM.scrollWrap.nextElementSibling;
DOM.bar = DOM.track.firstElementChild;
DOM.scope.classList.add("fakeScroll__scope");
return DOM;
},
destroy(){
this.events.off.call(this, window, 'resize', 'onScrollResize');
},
events : {
on(elm, eName, cbName){
// to be able tp unbind the events, callback refferece must be saved somewhere
eName.split(' ').forEach(e => {
if( !(cbName in this.events.callbacks) ) console.warn(cbName, " doesn't exist in Callbacks: ", this.events.callbacks);
this.listeners[e] = this.events.callbacks[cbName].bind(this);
elm.addEventListener(e, this.listeners[e])
});
return this.events;
},
off(elm, eName, cbName){
eName.split(' ').forEach(e => elm.removeEventListener(e, this.listeners[e]))
return this.events;
},
binding(DOM){
this.events.on.call(this, DOM.scrollContent, 'scroll', 'onScrollResize')
.on.call(this, DOM.scope, 'mouseenter', 'onScrollResize')
.on.call(this, DOM.bar, 'mousedown', 'onBarMouseDown')
.on.call(this, window, 'resize', 'onScrollResize')
if( this.settings.track )
this.events.on.call(this, DOM.track, 'click', 'onTrackClick')
},
/**
* events only binded when Bar element gets a "mousedown" event
* @param {[type]} onOff [description]
* @return {[type]} [description]
*/
drag(onOff){
this.events[onOff].call(this, document, 'mousemove', 'onDrag')
[onOff].call(this, document, 'mouseup', 'onStopDrag')
},
callbacks : {
onScrollResize(e){
this.moveBar.call(this);
// debounce - get track bounds
clearTimeout(this.listeners.timeout__resize);
this.listeners.timeout__resize = setTimeout(this.getTrackBounds.bind(this), 200)
},
onDrag(e){
var delta = e.pageY - this.state.lastPageY;
raf(() => {
var sTop = document.documentElement.scrollTop,
isDragWithinTrackBounds = e.pageY >= (this.state.trackBounds.top + sTop) && e.pageY <= (this.state.trackBounds.bottom + sTop);
if( isDragWithinTrackBounds )
this.DOM.scrollContent.scrollTop = this.state.drag + delta / this.state.scrollRatio;
// update variables when mouse position is outside the Track bounds
else{
this.state.drag = this.DOM.scrollContent.scrollTop;
this.state.lastPageY = e.pageY;
}
});
},
onStopDrag(e){
[this.DOM.bar, document.body].map(el => el.classList.remove('fakeScroll--grabbed'))
this.events.drag.call(this, 'off');
setTimeout(()=>{ this.state.drag = false })
},
onBarMouseDown(e){
this.state.drag = this.DOM.scrollContent.scrollTop;
this.state.lastPageY = e.pageY;
[this.DOM.bar, document.body].map(el => el.classList.add('fakeScroll--grabbed'))
this.events.drag.call(this, 'on');
},
onTrackClick(e){
if( this.state.drag ) return;
var perc = (e.clientY - this.state.trackBounds.top) / (this.state.trackBounds.height - this.state.trackBounds.topPad - this.state.trackBounds.bottomPad),
scrollHeight = this.DOM.scrollContent.scrollHeight,
ownHeight = this.DOM.scrollWrap.clientHeight,
newScrollTop = perc * (scrollHeight - ownHeight);
if( this.settings.track == 'smooth' ){
this.DOM.scrollContent.style.scrollBehavior = 'smooth';
setTimeout(()=>{ this.DOM.scrollContent.style.scrollBehavior = 'unset' }, 500)
}
this.DOM.scrollContent.scrollTop = newScrollTop;
}
}
},
getTrackBounds(){
var bounds = this.DOM.track.getBoundingClientRect(),
styles = window.getComputedStyle(this.DOM.track, null);
bounds.topPad = parseInt(styles.paddingTop, 10);
bounds.bottomPad = parseInt(styles.paddingBottom, 10);
this.state.trackBounds = bounds;
return bounds;
},
moveBar(){
// if( !this.DOM.scrollContent ) return false;
var _scrollContent = this.DOM.scrollContent,
scrollHeight = _scrollContent.scrollHeight,
ownHeight = this.DOM.scrollWrap.clientHeight;
this.state.scrollRatio = this.DOM.track.clientHeight / scrollHeight;
// update fake scrollbar location on the Y axis using requestAnimationFrame
raf(()=> {
var height = (ownHeight / scrollHeight) * 100,
top = (_scrollContent.scrollTop / scrollHeight ) * 100;
this.DOM.bar.style.cssText = `height : ${height}%;
top : ${top}%;
display : ${scrollHeight <= ownHeight ? 'none' : ''}`;
});
}
}
/**
* Extend the DOM with "fakeScroll" method. The chances of the same name already be taken are slim to none,
* But you should now; it's your code you're putting this into.
*/
Element.prototype.fakeScroll = function( settings ){
this._fakeScroll = this._fakeScroll || new FakeScroll(this, settings || {});
return this._fakeScroll;
}
})();