Skip to content

Commit 69b2dea

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

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

dragscroll.js

Lines changed: 68 additions & 11 deletions
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 <heliosframework@gmail.com>
6+
* @copyright 2015 asvd <heliosframework@gmail.com>
77
*/
88

99

@@ -25,6 +25,54 @@
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+
function stop_ticker(){
40+
clearTimeout(ticking);
41+
ticking = false;
42+
timestamp = Date.now();
43+
animation = requestAnimationFrame(autoScroll);
44+
45+
function tick(){
46+
if(ticking){
47+
ticking = setTimeout(tick, 100);
48+
velocity.x = last.x - current.x;
49+
velocity.y = last.y - current.y;
50+
last.x = current.x;
51+
last.y = current.y;
52+
}
53+
}
54+
55+
var timeConstant = 125; // ms
56+
57+
function scroll(x,y){
58+
window.scrollBy(x, y);
59+
}
60+
function autoScroll() {
61+
var elapsed, delta = {x:0, y:0}, dampening;
62+
target = {x: -0, y: -0};
63+
64+
if (velocity.x || velocity.y) {
65+
elapsed = Date.now() - timestamp;
66+
dampening = 1.5 * Math.exp(-elapsed / timeConstant)
67+
delta.x = -velocity.x * dampening;
68+
delta.y = -velocity.y * dampening;
69+
if (delta.x > 10 || delta.x < -10) {
70+
scroll(delta.x, delta.y);
71+
requestAnimationFrame(autoScroll);
72+
}
73+
}
74+
}
75+
2876
var dragged = [];
2977
var reset = function(i, el) {
3078
for (i = 0; i < dragged.length;) {
@@ -40,36 +88,45 @@
4088
el[addEventListener](
4189
mousedown,
4290
el.md = function(e) {
91+
cancelAnimationFrame(animation);
92+
document.body.classList.add("down");
4393
pushed = 1;
4494
lastClientX = e.clientX;
4595
lastClientY = e.clientY;
4696

97+
start_ticker();
98+
4799
e.preventDefault();
48100
e.stopPropagation();
49101
}, 0
50102
);
51-
103+
52104
_window[addEventListener](
53-
mouseup, el.mu = function() {pushed = 0;}, 0
105+
mouseup, el.mu = function() {
106+
document.body.classList.remove("down");
107+
stop_ticker();
108+
pushed = 0;
109+
}, 0
54110
);
55-
111+
56112
_window[addEventListener](
57113
mousemove,
58114
el.mm = function(e, scroller) {
59-
scroller = el.scroller||el;
115+
scroller = el.scroller || el;
60116
if (pushed) {
61-
scroller.scrollLeft -=
62-
(- lastClientX + (lastClientX=e.clientX));
63-
scroller.scrollTop -=
64-
(- lastClientY + (lastClientY=e.clientY));
117+
scrollX = lastClientX - (lastClientX=e.clientX);
118+
scrollY = lastClientY - (lastClientY=e.clientY);
119+
scroll(scrollX, scrollY);
120+
current.x += scrollX;
121+
current.y += scrollY;
65122
}
66123
}, 0
67124
);
68125
})(dragged[i++]);
69126
}
70127
}
71128

72-
129+
73130
if (_document.readyState == 'complete') {
74131
reset();
75132
} else {

0 commit comments

Comments
 (0)