Skip to content

Commit dd0d73d

Browse files
author
Johann Jacobsohn
committed
implement momentum scrolling
1 parent e71fe66 commit dd0d73d

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

dragscroll.js

+70-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @fileoverview dragscroll - scroll area by dragging
33
* @version 0.0.5
4-
*
4+
*
55
* @license MIT, see http://github.com/asvd/intence
6-
* @copyright 2015 asvd <[email protected]>
6+
* @copyright 2015 asvd <[email protected]>
77
*/
88

99

@@ -25,6 +25,56 @@
2525
var addEventListener = 'add'+EventListener;
2626
var removeEventListener = 'remove'+EventListener;
2727

28+
var ticking = false;
29+
var current = {x:0, y:0};
30+
var last = {x:0, y:0};
31+
var velocity = {x:0, y:0};
32+
var timestamp;
33+
var animation = 0;
34+
35+
function start_ticker(){
36+
ticking = true;
37+
tick();
38+
}
39+
40+
function stop_ticker(){
41+
clearTimeout(ticking);
42+
ticking = false;
43+
timestamp = Date.now();
44+
animation = requestAnimationFrame(autoScroll);
45+
}
46+
47+
function tick(){
48+
if(ticking){
49+
ticking = setTimeout(tick, 100);
50+
velocity.x = last.x - current.x;
51+
velocity.y = last.y - current.y;
52+
last.x = current.x;
53+
last.y = current.y;
54+
}
55+
}
56+
57+
var timeConstant = 125; // ms
58+
59+
function scroll(x,y){
60+
window.scrollBy(x, y);
61+
}
62+
function autoScroll() {
63+
var elapsed, delta = {x:0, y:0}, dampening;
64+
target = {x: -0, y: -0};
65+
66+
if (velocity.x || velocity.y) {
67+
elapsed = Date.now() - timestamp;
68+
dampening = 1.5 * Math.exp(-elapsed / timeConstant)
69+
delta.x = -velocity.x * dampening;
70+
delta.y = -velocity.y * dampening;
71+
if (delta.x > 10 || delta.x < -10) {
72+
scroll(delta.x, delta.y);
73+
requestAnimationFrame(autoScroll);
74+
}
75+
}
76+
}
77+
2878
var dragged = [];
2979
var reset = function(i, el) {
3080
for (i = 0; i < dragged.length;) {
@@ -40,36 +90,45 @@
4090
el[addEventListener](
4191
mousedown,
4292
el.md = function(e) {
93+
cancelAnimationFrame(animation);
94+
document.body.classList.add("down");
4395
pushed = 1;
4496
lastClientX = e.clientX;
4597
lastClientY = e.clientY;
4698

99+
start_ticker();
100+
47101
e.preventDefault();
48102
e.stopPropagation();
49103
}, 0
50104
);
51-
105+
52106
_window[addEventListener](
53-
mouseup, el.mu = function() {pushed = 0;}, 0
107+
mouseup, el.mu = function() {
108+
document.body.classList.remove("down");
109+
stop_ticker();
110+
pushed = 0;
111+
}, 0
54112
);
55-
113+
56114
_window[addEventListener](
57115
mousemove,
58116
el.mm = function(e, scroller) {
59-
scroller = el.scroller||el;
117+
scroller = el.scroller || el;
60118
if (pushed) {
61-
scroller.scrollLeft -=
62-
(- lastClientX + (lastClientX=e.clientX));
63-
scroller.scrollTop -=
64-
(- lastClientY + (lastClientY=e.clientY));
119+
scrollX = lastClientX - (lastClientX=e.clientX);
120+
scrollY = lastClientY - (lastClientY=e.clientY);
121+
scroll(scrollX, scrollY);
122+
current.x += scrollX;
123+
current.y += scrollY;
65124
}
66125
}, 0
67126
);
68127
})(dragged[i++]);
69128
}
70129
}
71130

72-
131+
73132
if (_document.readyState == 'complete') {
74133
reset();
75134
} else {

0 commit comments

Comments
 (0)