-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDragScroll.js
71 lines (61 loc) · 2.54 KB
/
DragScroll.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
define([
'specimenTools/_BaseWidget'
], function(
Parent
) {
"use strict";
function DragScroll(container, pubSub, options) {
Parent.call(this, options);
this._container = container;
this._pubSub = pubSub;
this._container.addEventListener('mousedown', this._initDrag.bind(this));
this._container.addEventListener('touchstart', this._initDrag.bind(this));
this.__endDrag = this._endDrag.bind(this);
this.__performMove = this._performMove.bind(this);
this.__move = this._move.bind(this);
this._startPos = null;
this._lastPos = null;
this._requestID = null;
}
var _p = DragScroll.prototype = Object.create(Parent.prototype);
_p.constructor = DragScroll;
DragScroll.defaultOptions = {
};
_p._initDrag = function(event) {
if('touches' in event)
event = event.touches[0];
this._startPos = {x: event.screenX, y:event.screenY};
this._container.ownerDocument.addEventListener('mouseup', this.__endDrag);
this._container.ownerDocument.addEventListener('touchend', this.__endDrag);
this._container.ownerDocument.addEventListener('mousemove', this.__move);
this._container.ownerDocument.addEventListener('touchmove', this.__move);
};
_p._endDrag =function() {
this._container.ownerDocument.removeEventListener('mouseup', this.__endDrag);
this._container.ownerDocument.removeEventListener('touchend', this.__endDrag);
this._container.ownerDocument.removeEventListener('mousemove', this.__move);
this._container.ownerDocument.removeEventListener('touchmove', this.__move);
this._startPos = null;
this._lastPos = null;
if(this._requestID !== null) {
window.cancelAnimationFrame(this._requestID);
this._requestID = null;
}
};
_p._performMove = function() {
this._container.scrollLeft = this._container.scrollLeft + this._startPos.x - this._lastPos.x;
this._container.scrollTop = this._container.scrollTop + this._startPos.y - this._lastPos.y;
this._requestID = null;
}
_p._move = function(event) {
if('touches' in event)
event = event.touches[0];
if(this._lastPos !== null)
this._startPos = this._lastPos;
this._lastPos = {x: event.screenX, y:event.screenY};
// throttle
if(this._requestID === null)
this._requestID = window.requestAnimationFrame(this.__performMove);
};
return DragScroll;
});