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

support devices : add touchstart touchend touchmove touchcancel events #7

Open
wants to merge 4 commits 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
63 changes: 57 additions & 6 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]>
*/


Expand All @@ -21,17 +21,39 @@
var mousemove = 'mousemove';
var mouseup = 'mouseup';
var mousedown = 'mousedown';
var touchstart = 'touchstart';
var touchend = 'touchend';
var touchmove = 'touchmove';
var touchcancel = 'touchcancel';
var EventListener = 'EventListener';
var addEventListener = 'add'+EventListener;
var removeEventListener = 'remove'+EventListener;

var dragged = [];

var off = function(i, el) {
for (i = 0; i < dragged.length;) {
el = dragged[i++];
el[removeEventListener](mousedown, el.md, 0);
el[removeEventListener](touchstart, el.tmd, 0);
_window[removeEventListener](mouseup, el.mu, 0);
_window[removeEventListener](mousemove, el.mm, 0);
_window[removeEventListener](touchmove, el.tmm, 0);
_window[removeEventListener](touchend, el.tmu, 0);
_window[removeEventListener](touchcancel, el.tmc, 0);
}
}

var reset = function(i, el) {
for (i = 0; i < dragged.length;) {
el = dragged[i++];
el[removeEventListener](mousedown, el.md, 0);
el[removeEventListener](touchstart, el.tmd, 0);
_window[removeEventListener](mouseup, el.mu, 0);
_window[removeEventListener](mousemove, el.mm, 0);
_window[removeEventListener](touchmove, el.tmm, 0);
_window[removeEventListener](touchend, el.tmu, 0);
_window[removeEventListener](touchcancel, el.tmc, 0);
}

dragged = _document.getElementsByClassName('dragscroll');
Expand All @@ -48,15 +70,30 @@
e.stopPropagation();
}, 0
);

el[addEventListener](
touchstart,
el.tmd = function(e) {
pushed = 1;
lastClientX = e.touches[0].clientX;
lastClientY = e.touches[0].clientY;

}, 0
);

_window[addEventListener](
mouseup, el.mu = function() {pushed = 0;}, 0
);

_window[addEventListener](
touchend, el.tmu = function() {pushed = 0;}, 0
);
_window[addEventListener](
touchcancel, el.tmc = function() {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));
Expand All @@ -65,17 +102,31 @@
}
}, 0
);

_window[addEventListener](
touchmove,
el.tmm = function(e, scroller) {
scroller = el.scroller||el;
if (pushed) {
scroller.scrollLeft -=
(- lastClientX + (lastClientX=e.touches[0].clientX));
scroller.scrollTop -=
(- lastClientY + (lastClientY=e.touches[0].clientY));
}
}, 0
);
})(dragged[i++]);
}
}


if (_document.readyState == 'complete') {
reset();
} else {
_window[addEventListener]('load', reset, 0);
}

exports.reset = reset;
exports.off = off;
}));