|
1 | 1 | /** |
2 | 2 | * @fileoverview dragscroll - scroll area by dragging |
3 | 3 | * @version 0.0.5 |
4 | | - * |
| 4 | + * |
5 | 5 | * @license MIT, see http://github.com/asvd/intence |
6 | | - * @copyright 2015 asvd <heliosframework@gmail.com> |
| 6 | + * @copyright 2015 asvd <heliosframework@gmail.com> |
7 | 7 | */ |
8 | 8 |
|
9 | 9 |
|
|
25 | 25 | var addEventListener = 'add'+EventListener; |
26 | 26 | var removeEventListener = 'remove'+EventListener; |
27 | 27 |
|
| 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 | + |
28 | 76 | var dragged = []; |
29 | 77 | var reset = function(i, el) { |
30 | 78 | for (i = 0; i < dragged.length;) { |
|
40 | 88 | el[addEventListener]( |
41 | 89 | mousedown, |
42 | 90 | el.md = function(e) { |
| 91 | + cancelAnimationFrame(animation); |
| 92 | + document.body.classList.add("down"); |
43 | 93 | pushed = 1; |
44 | 94 | lastClientX = e.clientX; |
45 | 95 | lastClientY = e.clientY; |
46 | 96 |
|
| 97 | + start_ticker(); |
| 98 | + |
47 | 99 | e.preventDefault(); |
48 | 100 | e.stopPropagation(); |
49 | 101 | }, 0 |
50 | 102 | ); |
51 | | - |
| 103 | + |
52 | 104 | _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 |
54 | 110 | ); |
55 | | - |
| 111 | + |
56 | 112 | _window[addEventListener]( |
57 | 113 | mousemove, |
58 | 114 | el.mm = function(e, scroller) { |
59 | | - scroller = el.scroller||el; |
| 115 | + scroller = el.scroller || el; |
60 | 116 | 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; |
65 | 122 | } |
66 | 123 | }, 0 |
67 | 124 | ); |
68 | 125 | })(dragged[i++]); |
69 | 126 | } |
70 | 127 | } |
71 | 128 |
|
72 | | - |
| 129 | + |
73 | 130 | if (_document.readyState == 'complete') { |
74 | 131 | reset(); |
75 | 132 | } else { |
|
0 commit comments