Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement momentum scrolling #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
implement momentum scrolling
Johann Jacobsohn committed Jul 16, 2015
commit dd0d73d49e7431c5138a8c0cbad2a92e144dfa26
81 changes: 70 additions & 11 deletions dragscroll.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @fileoverview dragscroll - scroll area by dragging
* @version 0.0.5
*
*
* @license MIT, see http://github.com/asvd/intence
* @copyright 2015 asvd <[email protected]>
* @copyright 2015 asvd <[email protected]>
*/


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

var ticking = false;
var current = {x:0, y:0};
var last = {x:0, y:0};
var velocity = {x:0, y:0};
var timestamp;
var animation = 0;

function start_ticker(){
ticking = true;
tick();
}

function stop_ticker(){
clearTimeout(ticking);
ticking = false;
timestamp = Date.now();
animation = requestAnimationFrame(autoScroll);
}

function tick(){
if(ticking){
ticking = setTimeout(tick, 100);
velocity.x = last.x - current.x;
velocity.y = last.y - current.y;
last.x = current.x;
last.y = current.y;
}
}

var timeConstant = 125; // ms

function scroll(x,y){
window.scrollBy(x, y);
}
function autoScroll() {
var elapsed, delta = {x:0, y:0}, dampening;
target = {x: -0, y: -0};

if (velocity.x || velocity.y) {
elapsed = Date.now() - timestamp;
dampening = 1.5 * Math.exp(-elapsed / timeConstant)
delta.x = -velocity.x * dampening;
delta.y = -velocity.y * dampening;
if (delta.x > 10 || delta.x < -10) {
scroll(delta.x, delta.y);
requestAnimationFrame(autoScroll);
}
}
}

var dragged = [];
var reset = function(i, el) {
for (i = 0; i < dragged.length;) {
@@ -40,36 +90,45 @@
el[addEventListener](
mousedown,
el.md = function(e) {
cancelAnimationFrame(animation);
document.body.classList.add("down");
pushed = 1;
lastClientX = e.clientX;
lastClientY = e.clientY;

start_ticker();

e.preventDefault();
e.stopPropagation();
}, 0
);

_window[addEventListener](
mouseup, el.mu = function() {pushed = 0;}, 0
mouseup, el.mu = function() {
document.body.classList.remove("down");
stop_ticker();
pushed = 0;
}, 0
);

_window[addEventListener](
mousemove,
el.mm = function(e, scroller) {
scroller = el.scroller||el;
scroller = el.scroller || el;
if (pushed) {
scroller.scrollLeft -=
(- lastClientX + (lastClientX=e.clientX));
scroller.scrollTop -=
(- lastClientY + (lastClientY=e.clientY));
scrollX = lastClientX - (lastClientX=e.clientX);
scrollY = lastClientY - (lastClientY=e.clientY);
scroll(scrollX, scrollY);
current.x += scrollX;
current.y += scrollY;
}
}, 0
);
})(dragged[i++]);
}
}


if (_document.readyState == 'complete') {
reset();
} else {