1.3.2 • Published 7 years ago

ng2draggable v1.3.2

Weekly downloads
256
License
ISC
Repository
github
Last release
7 years ago

draggable

Build Status Open Source Love

Angular 2 directive to make an element draggable. This is different from drag and drop as this is used to drag a div around. Live demo

Table of content

Install

https://www.npmjs.com/package/ng2draggable

  npm install ng2draggable

Or just copy draggable.directive.ts file content in a directive.

In any case you need to add the directive to your module.

import { Draggable } from 'ng2draggable/draggable.directive';
 
@NgModule({
  declarations: [
    ...,
    Draggable
  ],...})
  

Usage

<div [ng2-draggable]="true"></div>

That's it! now your component can be moved around.

You can disable it as well :

<div [ng2-draggable]="false"></div>

The component will have a custom class added to it as well: cursor-draggable. You can then use that css to costumize it :

/* Applies a grabbing hand on top of the div that's draggable*/
.cursor-draggable {
    cursor: grab;
    cursor: -moz-grab;
    cursor: -webkit-grab;
}
 /*  Apply a "closed-hand" cursor during drag operation. */
.cursor-draggable:active {
    cursor: grabbing;
    cursor: -moz-grabbing;
    cursor: -webkit-grabbing;
}

When the boolean input is false the class is removed and the component is no longer moveable.

Drag element with child element

The element that triggers the dragging can be specified by using ng2DraggableHandle. In the example below the element dragging is controlled by clicking and dragging on the element elementreference.

<div [ng2-draggable]=true [ng2DraggableHandle]="elementreference">
    <div #elementreference>
        Clicking and dragging this drags the parent element.
    </div>
    <div>
        Clicking and dragging this does not drag the parent element.
    </div>
</div>

Images

If there is an image in the children of the dragged component, make sure to add the HTML5 property draggable="false" to it. This property is used for drag and drop situations, and since ng2draggable directive is not based on that property for various reasons, letting it in might make the component behave unexpectedly.

Feedback

Your feedback is appreciated. If you wanna get involved you are welcome to.

You might be interested to follow me on twitter to be informed of future projects :) https://twitter.com/Ced_VDB

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.7

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.0

8 years ago