-
Notifications
You must be signed in to change notification settings - Fork 44
/
jquery.trackpad-scroll-emulator.js
329 lines (284 loc) · 9.44 KB
/
jquery.trackpad-scroll-emulator.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*!
* TrackpadScrollEmulator
* Version: 1.0.8
* Author: Jonathan Nicol @f6design
* https://github.com/jnicol/trackpad-scroll-emulator
*/
;(function($) {
var pluginName = 'TrackpadScrollEmulator';
function Plugin(element, options) {
var el = element;
var $el = $(element);
var $scrollContentEl;
var $contentEl = $el.find('.tse-content:first');
var $scrollbarEl;
var $dragHandleEl;
var dragOffset;
var flashTimeout;
var pageJumpMultp = 7/8;
var scrollDirection = 'vert';
var scrollOffsetAttr = 'scrollTop';
var sizeAttr = 'height';
var offsetAttr = 'top';
options = $.extend({}, $.fn[pluginName].defaults, options);
/**
* Initialize plugin
*/
function init() {
if ($el.hasClass('horizontal')){
scrollDirection = 'horiz';
scrollOffsetAttr = 'scrollLeft';
sizeAttr = 'width';
offsetAttr = 'left';
}
$el.prepend('<div class="tse-scrollbar"><div class="drag-handle"></div></div>');
$scrollbarEl = $el.find('.tse-scrollbar:first');
$dragHandleEl = $el.find('.drag-handle:first');
if (options.wrapContent) {
$contentEl.wrap('<div class="tse-scroll-content" />');
}
$scrollContentEl = $el.find('.tse-scroll-content:first');
resizeScrollContent();
if (options.autoHide) {
$el.on('mouseenter', flashScrollbar);
}
$dragHandleEl.on('mousedown', startDrag);
$scrollbarEl.on('mousedown', jumpScroll);
$scrollContentEl.on('scroll', onScrolled);
resizeScrollbar();
$(window).on('resize.trackpadScollEmulator', recalculate);
if (!options.autoHide) {
showScrollbar();
}
}
/**
* Start scrollbar handle drag
*/
function startDrag(e) {
// Preventing the event's default action stops text being
// selectable during the drag.
e.preventDefault();
// Measure how far the user's mouse is from the top of the scrollbar drag handle.
var eventOffset = e.pageY;
if (scrollDirection === 'horiz') {
eventOffset = e.pageX;
}
dragOffset = eventOffset - $dragHandleEl.offset()[offsetAttr];
$(document).on('mousemove', drag);
$(document).on('mouseup', endDrag);
}
/**
* Drag scrollbar handle
*/
function drag(e) {
e.preventDefault();
// Calculate how far the user's mouse is from the top/left of the scrollbar (minus the dragOffset).
var eventOffset = e.pageY;
if (scrollDirection === 'horiz') {
eventOffset = e.pageX;
}
var dragPos = eventOffset - $scrollbarEl.offset()[offsetAttr] - dragOffset;
// Convert the mouse position into a percentage of the scrollbar height/width.
var dragPerc = dragPos / $scrollbarEl[sizeAttr]();
// Scroll the content by the same percentage.
var scrollPos = dragPerc * $contentEl[sizeAttr]();
$scrollContentEl[scrollOffsetAttr](scrollPos);
}
/**
* End scroll handle drag
*/
function endDrag() {
$(document).off('mousemove', drag);
$(document).off('mouseup', endDrag);
}
/**
* Scroll in the same manner as the PAGE UP/DOWN keys
*/
function jumpScroll(e) {
// If the drag handle element was pressed, don't do anything here.
if (e.target === $dragHandleEl[0]) {
return;
}
// The content will scroll by 7/8 of a page.
var jumpAmt = pageJumpMultp * $scrollContentEl[sizeAttr]();
// Calculate where along the scrollbar the user clicked.
var eventOffset = (scrollDirection === 'vert') ? e.originalEvent.layerY : e.originalEvent.layerX;
// Get the position of the top (or left) of the drag handle.
var dragHandleOffset = $dragHandleEl.position()[offsetAttr];
// Determine which direction to scroll.
var scrollPos = (eventOffset < dragHandleOffset) ? $scrollContentEl[scrollOffsetAttr]() - jumpAmt : $scrollContentEl[scrollOffsetAttr]() + jumpAmt;
$scrollContentEl[scrollOffsetAttr](scrollPos);
}
/**
* Scroll callback
*/
function onScrolled(e) {
flashScrollbar();
}
/**
* Resize scrollbar
*/
function resizeScrollbar() {
var contentSize = sizeAttr === 'height' ? $contentEl.outerHeight() : $contentEl.outerWidth();
var scrollOffset = $scrollContentEl[scrollOffsetAttr](); // Either scrollTop() or scrollLeft().
var scrollbarSize = $scrollbarEl[sizeAttr]();
var scrollbarRatio = scrollbarSize / contentSize;
// Calculate new height/position of drag handle.
// Offset of 2px allows for a small top/bottom or left/right margin around handle.
var handleOffset = Math.round(scrollbarRatio * scrollOffset) + 2;
var handleSize = Math.floor(scrollbarRatio * (scrollbarSize - 2)) - 2;
if (scrollbarSize < contentSize) {
if (scrollDirection === 'vert'){
$dragHandleEl.css({'top': handleOffset, 'height': handleSize});
} else {
$dragHandleEl.css({'left': handleOffset, 'width': handleSize});
}
$scrollbarEl.show();
} else {
$scrollbarEl.hide();
}
}
/**
* Flash scrollbar visibility
*/
function flashScrollbar() {
resizeScrollbar();
showScrollbar();
}
/**
* Show scrollbar
*/
function showScrollbar() {
$dragHandleEl.addClass('visible');
if (!options.autoHide) {
return;
}
if(typeof flashTimeout === 'number') {
window.clearTimeout(flashTimeout);
}
flashTimeout = window.setTimeout(function() {
hideScrollbar();
}, 1000);
}
/**
* Hide Scrollbar
*/
function hideScrollbar() {
$dragHandleEl.removeClass('visible');
if(typeof flashTimeout === 'number') {
window.clearTimeout(flashTimeout);
}
}
/**
* Resize content element
*/
function resizeScrollContent() {
if (scrollDirection === 'vert'){
$scrollContentEl.width($el.width()+scrollbarWidth());
$scrollContentEl.height($el.height());
} else {
$scrollContentEl.width($el.width());
$scrollContentEl.height($el.height()+scrollbarWidth());
$contentEl.height($el.height());
}
}
/**
* Calculate scrollbar width
*
* Original function by Jonathan Sharp:
* http://jdsharp.us/jQuery/minute/calculate-scrollbar-width.php
* Updated to work in Chrome v25.
*/
function scrollbarWidth() {
// Append a temporary scrolling element to the DOM, then measure
// the difference between between its outer and inner elements.
var tempEl = $('<div class="scrollbar-width-tester" style="width:50px;height:50px;overflow-y:scroll;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
$('body').append(tempEl);
var width = $(tempEl).innerWidth();
var widthMinusScrollbars = $('div', tempEl).innerWidth();
tempEl.remove();
// On OS X if the scrollbar is set to auto hide it will have zero width. On webkit we can still
// hide it using ::-webkit-scrollbar { width:0; height:0; } but there is no moz equivalent. So we're
// forced to sniff Firefox and return a hard-coded scrollbar width. I know, I know...
if (width === widthMinusScrollbars && navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
return 17;
}
return (width - widthMinusScrollbars);
}
/**
* Recalculate scrollbar
*/
function recalculate() {
resizeScrollContent();
resizeScrollbar();
}
/**
* Get/Set plugin option.
*/
function option (key, val) {
if (val) {
options[key] = val;
} else {
return options[key];
}
}
/**
* Destroy plugin.
*/
function destroy() {
// Restore the element to its original state.
$contentEl.insertBefore($scrollbarEl);
$scrollbarEl.remove();
$scrollContentEl.remove();
$contentEl.css({'height': $el.height()+'px', 'overflow-y': 'scroll'});
$(window).off('resize.trackpadScollEmulator');
hook('onDestroy');
$el.removeData('plugin_' + pluginName);
}
/**
* Plugin callback hook.
*/
function hook(hookName) {
if (options[hookName] !== undefined) {
options[hookName].call(el);
}
}
init();
return {
option: option,
destroy: destroy,
recalculate: recalculate
};
}
$.fn[pluginName] = function(options) {
if (typeof arguments[0] === 'string') {
var methodName = arguments[0];
var args = Array.prototype.slice.call(arguments, 1);
var returnVal;
this.each(function() {
if ($.data(this, 'plugin_' + pluginName) && typeof $.data(this, 'plugin_' + pluginName)[methodName] === 'function') {
returnVal = $.data(this, 'plugin_' + pluginName)[methodName].apply(this, args);
} else {
throw new Error('Method ' + methodName + ' does not exist on jQuery.' + pluginName);
}
});
if (returnVal !== undefined){
return returnVal;
} else {
return this;
}
} else if (typeof options === "object" || !options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
}
};
$.fn[pluginName].defaults = {
onInit: function() {},
onDestroy: function() {},
wrapContent: true,
autoHide: true
};
})(jQuery);