Repository navigation
Expand file tree
/
Copy pathscrollfoo.js
More file actions
276 lines (226 loc) · 8.29 KB
/
Copy pathscrollfoo.js
File metadata and controls
276 lines (226 loc) · 8.29 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* ScrollFoo, a jQuery + BEM-friendly scrollbar plugin v0.1
*
* ==============================
* DOM
* ==============================
* <div class="scrollfoo__content-wrapper">
* <!-- Scrollbar element -->
* <span class="scrollfoo__scroller scrollfoo__scroller--{{custom-modifier}}"></span>
* <!-- Parent element with overflow: hidden -->
* <div class="scrollfoo__parent scrollfoo__parent--{{custom-modifier}}"></div>
* </div>
*
* ==============================
* Initialization
* ==============================
* window.newScrollFooInstance = new ScrollFoo({
* parentEl: '{{parent-element}}',
* scrollerEl: '.scrollfoo__scroller--{{custom-modifier}}',
* visibleParentHeight: 300,
* realParentHeight: function() {
* return $('.scrollfoo__parent--{{custom-modifier}}').outerHeight();
* }
* });
*
* @link https://github.com/vslio/scrollFoo
* @author Kostas Vasileiou <hello@vsl.io>
* @license http://opensource.org/licenses/MIT MIT
*/
/**
* ScrollFoo Constructor
*/
var ScrollFoo = function(options) {
this.options = options || {};
this.defaults = {
// Element where the scrolling is active
parentEl: '',
// Scrollbar element
scrollerEl: '.scrollfoo__scroller',
// Visible height of parent element
visibleParentHeight: 0,
// Real height of parent element
realParentHeight: 0,
// Storing the visible content height to
// real container height ratio
ratio: 0,
// Setting ScrollFoo active/inactive
state: false,
// Give the parent some room
visibleParentHeightOffset: 0,
// Added to the content wrapper element
// when we're dragging the scrollbar
draggingClass: 'scrollfoo--dragging',
disabledClass: 'scrollfoo__scroller--disabled'
};
// Caching the body element
this.$body = $('body');
// Merging user options with plugin defaults
this.config = $.extend(true, {}, this.defaults, this.options);
this.initialize();
};
ScrollFoo.prototype.initialize = function() {
this.registerEvents();
this.doCalculate();
};
ScrollFoo.prototype.registerEvents = function() {
var self = this;
this.unload();
// ScrollFoo scroll event
$(this.config.parentEl).on('scroll mousewheel DOMMouseScroll MozMousePixelScroll', function(ev) {
self.onScrollEvent($(this), ev);
});
// Dragging the scrollbar
$(this.config.scrollerEl).on('mousedown', function(ev) {
self.onMouseDownEvent($(this), ev);
});
};
ScrollFoo.prototype.unload = function() {
$(this.config.parentEl).off('scroll mousewheel DOMMouseScroll MozMousePixelScroll');
$(this.config.scrollerEl).off('mousedown');
};
/**
* Method called when scrolling inside a ScrollFoo enabled element
*
* @param {jQuery} $target ScrollFoo element
* @param {Object} ev mousewheel/DOMMouseScroll/MozMousePixelScroll event object
*/
ScrollFoo.prototype.onScrollEvent = function($target, ev) {
var scrolledPos;
if (ev.type === 'scroll') {
scrolledPos = $target.scrollTop();
if (typeof ev.originalEvent.wheelDeltaY == 'undefined') {
if (this.config.state === true) {
this.handleReelbarScroller(scrolledPos, false);
}
}
} else {
if (ev.type !== 'DOMMouseScroll' && ev.type !== 'MozMousePixelScroll') {
var wheelDelta;
// There's no wheelDeltaY on IE
if (ev.originalEvent.wheelDeltaY) {
wheelDelta = ev.originalEvent.wheelDeltaY;
} else {
wheelDelta = ev.originalEvent.wheelDelta;
}
if (wheelDelta > 0) {
scrolledPos = $target.scrollTop() - Math.abs((wheelDelta));
$target.scrollTop($target.scrollTop() - Math.abs((wheelDelta/10)));
} else {
scrolledPos = $target.scrollTop() + Math.abs((wheelDelta));
$target.scrollTop($target.scrollTop() + Math.abs((wheelDelta/10)));
}
} else {
var scrollDelta;
if (ev.type === 'MozMousePixelScroll') {
scrollDelta = ev.originalEvent.detail / 3;
} else {
scrollDelta = ev.originalEvent.detail * 3;
}
if (scrollDelta > 0) {
scrolledPos = $target.scrollTop($target.scrollTop() + Math.abs(scrollDelta));
} else {
scrolledPos = $target.scrollTop($target.scrollTop() - Math.abs(scrollDelta));
}
}
}
ev.preventDefault();
};
/**
* Method called when dragging a ScrollFoo scrollbar
*
* @param {jQuery} $target ScrollFoo scrollbar element
* @param {Object} ev Scroll event object
*/
ScrollFoo.prototype.onMouseDownEvent = function($target, ev) {
var initialY = ev.pageY;
var visibleParentHeight = this.getVisibleParentHeight();
var initialScrolledPos = parseFloat($target.css('top').replace('px', ''), 10) || 0;
var scrollerHeight = $target.height() - (this.config.visibleParentHeightOffset / 2);
// Adding our dragging class to the body just in
// case we want to apply specific style overrides
this.$body.addClass(this.config.draggingClass);
this.$body.on('mousemove', function(e) {
var finalScrolledPos = (e.pageY - initialY) + initialScrolledPos;
if (finalScrolledPos <= visibleParentHeight - scrollerHeight) {
if (finalScrolledPos < 0) {
finalScrolledPos = 0;
}
$target.css('top', finalScrolledPos);
$(this.config.parentEl).scrollTop(finalScrolledPos / this.config.ratio);
}
}.bind(this));
this.$body.one('mouseup', function() {
this.$body.off('mousemove');
$target.off('mouseup');
this.$body.removeClass(this.config.draggingClass);
}.bind(this));
};
ScrollFoo.prototype.getVisibleParentHeight = function() {
if (typeof this.config.visibleParentHeight === 'function') {
if (this.config.visibleParentHeight() === false) {
return 'auto';
} else {
return this.config.visibleParentHeight() + this.config.visibleParentHeightOffset;
}
} else {
return this.config.visibleParentHeight + this.config.visibleParentHeightOffset;
}
};
ScrollFoo.prototype.getRealParentHeight = function() {
if (typeof this.config.realParentHeight === 'function') {
return this.config.realParentHeight();
} else {
return this.config.realParentHeight;
}
};
/**
* Calculating container height and visible content height
* to real container height ratio, activating scrollbar if necessary
*
* @return {Object} ScrollFoo instance
*/
ScrollFoo.prototype.doCalculate = function() {
var $parentEl = $(this.config.parentEl);
var $scrollerEl = $(this.config.scrollerEl);
var visibleParentHeight, realParentHeight;
// Reset the visible container height, but
// store the current scrollposition first.
var currentScrolledPos = $parentEl.scrollTop();
$parentEl.css({ height: 'auto' });
visibleParentHeight = this.getVisibleParentHeight();
realParentHeight = this.getRealParentHeight();
this.config.ratio = visibleParentHeight / realParentHeight;
if (this.config.ratio < 1) {
var scrollbarHeight = this.config.ratio * visibleParentHeight;
this.config.state = true;
$scrollerEl
.css({ height: scrollbarHeight })
.removeClass(this.config.disabledClass);
} else {
this.config.state = false;
$scrollerEl.addClass(this.config.disabledClass);
}
$parentEl
.css({ height: visibleParentHeight })
.scrollTop(currentScrolledPos);
return this;
};
/**
* Set the scrollbar position
*
* @param {Number} scrolledPos Amount of pixels scrolled
* @param {Boolean} isMouseWheel Use of mousewheel or touchpad
* @return {Object} ScrollFoo instance
*/
ScrollFoo.prototype.handleReelbarScroller = function(scrolledPos, isMouseWheel) {
var isMouseWheel = isMouseWheel || false;
var scrollbarPos = {
top: scrolledPos * this.config.ratio
};
if (isMouseWheel) {
$(this.config.parentEl).css('top', - scrolledPos * this.config.ratio);
}
$(this.config.scrollerEl).css(scrollbarPos);
return this;
};