-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
442 lines (352 loc) · 10.7 KB
/
index.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* By Daniel Lever - daniel@ed.com.au
*/
var install = (function ($) {
/*
* http://paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 30);
};
})();
var AXIS_X = 1;
var AXIS_Y = 2;
var MODE_SCROLL = 1;
var MODE_POSITION = 2;
$.fn.disableScroll = function() {
return this.each(function() {
var handleWheel = function(e) {
e.stopPropagation();
e.preventDefault();
};
$(this).bind("mousewheel", handleWheel);
$(this).bind("DOMMouseScroll", handleWheel);
});
};
$.fn.scroller = function(options) {
var args = arguments;
var returnValue;
this.each(function(index) {
var scroller = $.data(this, 'scroller');
// Have specified a method to call
if(typeof args[0] == 'string') {
if(!scroller) {
throw new Error("Called scroller method '"+args[0]+"' before actually initializing one.");
} else if (args[0] in scroller === false) {
throw new Error("Attempted to call invalid scroller method '"+args[0]+"'");
} else {
var methodArgs = Array.prototype.slice.call(args, 1);
returnValue = scroller[args[0]].apply(scroller, methodArgs);
}
} else if(options === undefined || typeof options == 'object') {
// Supplied options
if(!scroller) {
// No scroller, so create one
createScroller($(this), options || {});
} else {
// Scroller already exists, just update it's options
scroller.options(options);
}
}
});
return returnValue === undefined ? this : returnValue;
};
var createScroller = function(scrollEl, options) {
// Options
options = jQuery.extend({
// Elements
receiver: scrollEl,
content: scrollEl,
frame: scrollEl,
// Options
axis: 'y', // either X or Y
mode: 'scroll', // either 'scroll', 'position' or 'none'
mousewheel: true, // Enable/disable mouse wheel scrolling
mouseDrag: false, // Allow mouse dragging (click/drag)
touchDrag: true, // Mouse dragging, but with touch only
autostartTouch: true, // Set this to `false` if you'd like to manually call `touchStart(e)`
friction: 0.9, // higher number means faster slow down
stepSize: 1, // step size
minMovement: 0.1, // minimum velocity before actually moving. 0.1 is a sensible default
// State
disabled: false,
// Callbacks
getCurrentPosition: null, // Should return a number less than zero, the current scroll offset
getContentSize: null, // Should return a number - the size of the scrollable content
getFrameSize: null, // Should return a number - the size of the scrollable area,
// Events
update: null, // Called every time the scroll position changes. First argument is the scroll offset in pixels, second argument is the scroll progress (between 0 and 1)
}, options);
// Runtime vars
var running = false,
currentPos = 0,
targetPos = 0,
lastPos = 0,
minScroll = 0,
maxScroll = 0,
direction,
velocity = 0,
contentSize = 0,
frameSize = 0,
axisID = (options.axis == 'x' && AXIS_X) || (options.axis == 'y' && AXIS_Y),
modeID = (options.mode == 'scroll' && MODE_SCROLL) || (options.mode == 'position' && MODE_POSITION),
isDragging = false,
dragStartTimer = 0,
dragStartWaiting = false,
dragStartPos = 0,
dragStartPagePos = 0,
lastDragPos = 0,
dragSpeed = 0;
var updateScrollTarget = function(delta) {
targetPos += delta;
velocity += (targetPos - lastPos) * options.stepSize;
lastPos = targetPos;
};
var render = function(force) {
if(velocity < -(options.minMovement) || velocity > options.minMovement || force) {
updateScrollSize();
currentPos = (currentPos + velocity);
if(maxScroll > 0) {
// Content is smaller than scrollable area
currentPos = 0;
velocity = 0;
} else if(currentPos < maxScroll) {
// Have scrolled too far forward
velocity = 0;
currentPos = maxScroll;
} else if (currentPos > minScroll) {
// Scrolled too far backward
velocity = 0;
currentPos = minScroll;
}
if(modeID == MODE_SCROLL) {
if(axisID == AXIS_Y) {
options.frame.scrollTop(-currentPos);
} else if(axisID == AXIS_X) {
options.frame.scrollLeft(-currentPos);
}
} else if(modeID == MODE_POSITION) {
if(axisID == AXIS_Y) {
options.content.css('top', currentPos+'px');
} else if(axisID == AXIS_X) {
options.content.css('left', currentPos+'px');
}
}
if(options.update) {
options.update(currentPos, currentPos/maxScroll || 0, maxScroll);
}
// Apply friction
velocity *= options.friction;
}
};
// Updates internal value of current position
var updatePosition = function() {
if(modeID == MODE_SCROLL) {
if(axisID == AXIS_Y) {
currentPos = -options.frame.scrollTop();
} else if(axisID == AXIS_X) {
currentPos = -options.frame.scrollLeft();
}
} else if(modeID == MODE_POSITION) {
if(axisID == AXIS_Y) {
currentPos = parseFloat(options.content[0].style.top) || 0;
} else if(axisID == AXIS_X) {
currentPos = parseFloat(options.content[0].style.left) || 0;
}
}
};
var animateLoop = function() {
if(!running) return;
requestAnimFrame(animateLoop);
render();
};
var handleWheel = function(e) {
if(options.disabled) return;
e.stopPropagation();
e.preventDefault();
var evt = e.originalEvent;
var delta = evt.detail ? evt.detail * -1 : evt.wheelDelta / 40;
var dir = delta < 0 ? -1 : 1;
if(dir != direction) {
velocity = 0;
direction = dir;
}
// Update currentPos in case user has scrolled with scroll bar
updatePosition();
// Update scroll target based on delta
updateScrollTarget(delta);
};
var updateScrollSize = function() {
// Get frame size
if(options.getFrameSize) {
// Custom frame size callback
frameSize = options.getFrameSize();
} else {
// Get frame frame el
if(axisID == AXIS_Y) {
frameSize = options.frame[0].clientHeight;
} else if(axisID == AXIS_X) {
frameSize = options.frame[0].clientWidth;
}
}
// Get content size
if(options.getContentSize) {
// Custom content size callback
contentSize = options.getContentSize();
} else if(modeID == MODE_SCROLL) {
// Get native scroll size of the frame el
if(axisID == AXIS_Y) {
contentSize = options.frame[0].scrollHeight;
} else if(axisID == AXIS_X) {
contentSize = options.frame[0].scrollWidth;
}
} else if(modeID == MODE_POSITION) {
// Get the width or height of the content el
if(axisID == AXIS_Y) {
contentSize = options.content[0].clientHeight;
} else if(axisID == AXIS_X) {
contentSize = options.content[0].clientWidth;
}
}
maxScroll = -(contentSize - frameSize);
};
// Bind mousewheel events
if(options.mousewheel === true) {
options.receiver.bind("mousewheel", handleWheel);
options.receiver.bind("DOMMouseScroll", handleWheel);
updatePosition();
targetPos = lastPos = currentPos;
updateScrollSize();
if(options.remove) {
running = false;
options.receiver.unbind("mousewheel", handleWheel);
options.receiver.unbind("DOMMouseScroll", handleWheel);
} else if(!running) {
running = true;
animateLoop();
}
}
var eventAxis = options.axis == 'y' ? 'pageY' : 'pageX';
var touchStartFunc;
if(options.touchDrag && ('ontouchstart' in window)) {
touchStartFunc = function(e) {
if(e.originalEvent.touches) {
var touch = e.originalEvent.touches[0];
isDragging = true;
dragStartPagePos = touch[eventAxis];
dragStartPos = currentPos;
lastDrag = 0;
dragSpeed = 0;
}
};
options.receiver.bind("touchstart", function(e) {
if(options.disabled) return;
if(options.autostartTouch) {
// e.preventDefault();
// e.stopPropagation();
dragStartWaiting = e;
dragStartTimer = setTimeout(function() {
touchStartFunc(e);
}, 200);
}
});
$(window).bind("touchend touchcancel touchleave", function(e) {
if(dragStartWaiting) {
dragStartWaiting = false;
clearInterval(dragStartTimer);
}
if(isDragging) {
e.preventDefault();
e.stopPropagation();
isDragging = false;
velocity = -dragSpeed;
dragSpeed = 0;
}
}).bind("touchmove", function(e) {
if(dragStartWaiting) {
touchStartFunc(dragStartWaiting);
dragStartWaiting = false;
clearInterval(dragStartTimer);
}
if(isDragging) {
var touch = e.originalEvent.touches[0];
dragSpeed = lastDragPos - touch[eventAxis];
lastDragPos = touch[eventAxis];
currentPos = dragStartPos - (dragStartPagePos - touch[eventAxis]);
render(true);
}
});
}
var mouseStartFunc;
if(options.mouseDrag) {
mouseStartFunc = function(e) {
if(e.pageX || e.pageY) {
isDragging = true;
dragStartPagePos = e[eventAxis];
dragStartPos = currentPos;
lastDrag = 0;
dragSpeed = 0;
}
};
options.receiver.bind("mousedown", function(e) {
if(options.autostartTouch) {
e.preventDefault();
e.stopPropagation();
mouseStartFunc(e);
}
});
$(window).bind("mouseup", function(e) {
e.preventDefault();
e.stopPropagation();
isDragging = false;
velocity = -dragSpeed;
dragSpeed = 0;
}).bind("mousemove", function(e) {
if(isDragging) {
dragSpeed = lastDragPos - e[eventAxis];
lastDragPos = e[eventAxis];
currentPos = dragStartPos - (dragStartPagePos - e[eventAxis]);
render(true);
}
});
}
var scroller = {
refresh: function() {
updatePosition();
render(true);
animateLoop();
},
disable: function() {
options.disabled = true;
},
enable: function() {
options.disabled = false;
},
startTouch: function(e) {
if(touchStartFunc) touchStartFunc(e);
if(mouseStartFunc) touchStartFunc(e);
},
destroy: function() {
options.disabled = true;
$.data(scrollEl[0], 'scroller', null);
},
getScrollSize: function() {
updateScrollSize();
return {contentSize: contentSize, frameSize: frameSize, currentPos: currentPos};
}
};
window.scroller = scroller;
$.data(scrollEl[0], 'scroller', scroller);
};
});
if(typeof jQuery === "object") {
install(jQuery);
} else if(typeof module === "object") {
module.exports = install;
}