0.0.1 • Published 7 years ago

angular-swipe-element v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
7 years ago

angular-swipe-element

Small extension for AngularJS to add wider support of swipes and drags

Live Demo here.

Basic usage

Define module as dependency!

var app = angular.module('app', ['angular-swipe-element']);

Define the directive!

<div swipe-element="onSwipeEnd(dir, deltaX, deltaY)" 
     swipe-element-move="onSwipeMove(dir, deltaX, deltaY)" 
     swipe-element-start="onSwipeStart(dir, deltaX, deltaY)"></div>

Define the callbacks in controller!

app.controller(function($scope) {
    $scope.onSwipeStart = function(dir, deltaX, deltaY) { console.log('Started, direction: ' + dir + '. DeltaX: ' + deltaX + '; DeltaY: ' + deltaY); };
    $scope.onSwipeMove = function(dir, deltaX, deltaY) { console.log('Moved, direction: ' + dir + '. DeltaX: ' + deltaX + '; DeltaY: ' + deltaY); };
    $scope.onSwipeEnd = function(dir, deltaX, deltaY) { console.log('Released, direction: ' + dir + '. DeltaX: ' + deltaX + '; DeltaY: ' + deltaY); };
});

You are ready! Enjoy the plugin.

Directive attributes

Directive can be used to catch swipe(mouse drag) start/move/end events. You can pass callbacks for these events in the following attributes:

AttributeRequiredDescription
swipe-elementRequiredFires when user stops swiping/dragging current element
swipe-element-startoptionalFires when user touches/mousedowns current element
swipe-element-moveoptionalFires when user moves finger/mouse while holding current element

Callback parameter

When some event is fired, directive callback's scopes are provided with $event parameter as well as with the following properties which could be used for your needs:

PropertyDescription
$eventContains all of the data properties
xCurrent X coordinate of the pointer in the document
yCurrent X coordinate of the pointer in the document
deltaXDifference on the X axis, comparing to swipe/drag starting point. (currentPosition - startingPosition)
deltaXDifference on the Y axis, comparing to swipe/drag starting point. (currentPosition - startingPosition)
dirString value representing direction, in which swipe/drag is heading. Can be equal to '', 'u', 'd', 'l', 'r'. If deltaX and deltaY equal to each other and not equal 0 takes horizontal value.
hDirString value representing direction, in which swipe/drag is heading on X axis. Can be equal to '', 'l', 'r'.
vDirString value representing direction, in which swipe/drag is heading on Y axis. Can be equal to '', 'u', 'd'.
startXX coordinate of the pointer in the document, when swipe/drag started
startYY coordinate of the pointer in the document, when swipe/drag started
initialElementBoundsgetClientBoundingRect() executed on element when the swipe/drag started.
triggerTypeCould be used to differ mouse and touch events. String value. Can be 'mouse' or 'touch'.
sourceSource DOM event.

License

MIT license.

Contribution

You are welcome to contribute. Feel free to contact me.