Skip to content

Support for mobile touch scroll #23

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ Use the axis attribute to lock the dragging to a specific axis. By default, both
```

### Events
Use the onDragStart and onDragEnd events to call functions whenever the dragging starts or stops
Use the on-drag-start and on-drag-end events to call functions whenever the dragging starts or stops
```html
<!-- Calls a function on start and stop -->
<div drag-scroll onDragStart="handleDragStart()" onDragEnd="handleDragEnd()">
<div drag-scroll on-drag-start="handleDragStart()" on-drag-end="handleDragEnd()">
```


Expand Down
105 changes: 94 additions & 11 deletions src/ng-drag-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

// Set event listeners
$element.on('mousedown', handleMouseDown);
$element.on('touchstart', handleTouchStart);

// Set destroy listener
$scope.$on('$destroy', destroy);
Expand All @@ -42,6 +43,9 @@
function setDragListeners () {
angular.element($window).on('mouseup', handleMouseUp);
angular.element($window).on('mousemove', handleMouseMove);

angular.element($window).on('touchend', handleTouchEnd);
angular.element($window).on('touchmove', handleTouchMove);
}

/**
Expand All @@ -50,15 +54,18 @@
function removeDragListeners () {
angular.element($window).off('mouseup', handleMouseUp);
angular.element($window).off('mousemove', handleMouseMove);

angular.element($window).off('touchend', handleTouchEnd);
angular.element($window).off('touchmove', handleTouchMove);
}

/**
* Handles mousedown event
* @param {object} e MouseDown event
*/
function handleMouseDown (e) {
if(enabled){
for (var i= 0; i<excludedClasses.length; i++) {
if (enabled) {
for (var i = 0; i < excludedClasses.length; i++) {
if (angular.element(e.target).hasClass(excludedClasses[i])) {
return false;
}
Expand Down Expand Up @@ -89,16 +96,16 @@
* @param {object} e MouseUp event
*/
function handleMouseUp (e) {
if(enabled){
var selectable = (e.target.attributes && 'drag-scroll-text' in e.target.attributes);
if (enabled) {
var selectable = ('drag-scroll-text' in e.target.attributes);
var withinXConstraints = (e.clientX >= (startClientX - allowedClickOffset) && e.clientX <= (startClientX + allowedClickOffset));
var withinYConstraints = (e.clientY >= (startClientY - allowedClickOffset) && e.clientY <= (startClientY + allowedClickOffset));

// Set 'pushed' state
pushed = false;

// Check if cursor falls within X and Y axis constraints
if(selectable && withinXConstraints && withinYConstraints) {
if (selectable && withinXConstraints && withinYConstraints) {
// If so, select the text inside the target element
selectText(e.target);
}
Expand All @@ -117,12 +124,12 @@
* @param {object} e MouseMove event
*/
function handleMouseMove (e) {
if(enabled){
if (enabled) {
if (pushed) {
if(!axis || axis === 'x') {
if (!axis || axis === 'x') {
$element[0].scrollLeft -= (-lastClientX + (lastClientX = e.clientX));
}
if(!axis || axis === 'y') {
if (!axis || axis === 'y') {
$element[0].scrollTop -= (-lastClientY + (lastClientY = e.clientY));
}
}
Expand All @@ -131,13 +138,89 @@
}
}

/**
* Handles touchstart event
* @param {object} e TouchStart event
*/
function handleTouchStart (e) {
if (enabled) {
for (var i = 0; i < excludedClasses.length; i++) {
if (angular.element(e.target).hasClass(excludedClasses[i])) {
return false;
}
}

$scope.$apply(function() {
onDragStart($scope);
});

// Set mouse drag listeners
setDragListeners();

// Set 'pushed' state
pushed = true;
lastClientX = startClientX = e.touches[0].clientX;
lastClientY = startClientY = e.touches[0].clientY;

clearSelection();
}
}

/**
* Handles touchend event
* @param {object} e TouchEnd event
*/
function handleTouchEnd (e) {
if (enabled) {
var selectable = ('drag-scroll-text' in e.target.attributes);
var withinXConstraints = (e.changedTouches[0].clientX >= (startClientX - allowedClickOffset) && e.changedTouches[0].clientX <= (startClientX + allowedClickOffset));
var withinYConstraints = (e.changedTouches[0].clientY >= (startClientY - allowedClickOffset) && e.changedTouches[0].clientY <= (startClientY + allowedClickOffset));

// Set 'pushed' state
pushed = false;

// Check if cursor falls within X and Y axis constraints
if (selectable && withinXConstraints && withinYConstraints) {
// If so, select the text inside the target element
selectText(e.target);
}

$scope.$apply(function() {
onDragEnd($scope);
});

removeDragListeners();
}
}

/**
* Handles touchmove event
* @param {object} e TouchMove event
*/
function handleTouchMove (e) {
if (enabled) {
if (pushed) {
if (!axis || axis === 'x') {
$element[0].scrollLeft -= (-lastClientX + (lastClientX = e.changedTouches[0].clientX));
}
if (!axis || axis === 'y') {
$element[0].scrollTop -= (-lastClientY + (lastClientY = e.changedTouches[0].clientY));
}
}
}
}

/**
* Destroys all the event listeners
*/
function destroy () {
$element.off('mousedown', handleMouseDown);
angular.element($window).off('mouseup', handleMouseUp);
angular.element($window).off('mousemove', handleMouseMove);

$element.on('touchstart', handleTouchStart);
angular.element($window).off('touchend', handleTouchEnd);
angular.element($window).off('touchmove', handleTouchMove);
}

/**
Expand All @@ -162,12 +245,12 @@
*/
function clearSelection () {
if ($window.getSelection) {
if ($window.getSelection().empty) { // Chrome
if ($window.getSelection().empty) { // Chrome
$window.getSelection().empty();
} else if ($window.getSelection().removeAllRanges) { // Firefox
} else if ($window.getSelection().removeAllRanges) { // Firefox
$window.getSelection().removeAllRanges();
}
} else if ($document.selection) { // IE?
} else if ($document.selection) { // IE?
$document.selection.empty();
}
}
Expand Down