How to implement a draggable div in Angular 2 using Rx

前端 未结 4 459
醉梦人生
醉梦人生 2020-12-24 07:43

I\'ve been trying to get a draggable div working using Angular 2. I\'m using this example from the angular2-examples repo as a starting point, only really adjusting the cod

4条回答
  •  时光取名叫无心
    2020-12-24 08:23

    You can use this : npm install ng2draggable

    Use [ng2-draggable]="true", don't forget the ="true"

    You can find it here

    https://github.com/cedvdb/ng2draggable

    Here is the code:

    @Directive({
      selector: '[ng2-draggable]'
    })
    export class Draggable implements OnInit{
        topStart:number=0;
        leftStart:number=0;
        _allowDrag:boolean = true;
        md:boolean;
    
        constructor(public element: ElementRef) {}
    
            ngOnInit(){
              // css changes
              if(this._allowDrag){
                this.element.nativeElement.style.position = 'relative';
                this.element.nativeElement.className += ' cursor-draggable';
              }
            }
    
            @HostListener('mousedown', ['$event'])
            onMouseDown(event:MouseEvent) {
              if(event.button === 2)
                return; // prevents right click drag, remove his if you don't want it
              this.md = true;
              this.topStart = event.clientY - this.element.nativeElement.style.top.replace('px','');
              this.leftStart = event.clientX - this.element.nativeElement.style.left.replace('px','');
            }
    
            @HostListener('document:mouseup')
            onMouseUp(event:MouseEvent) {
              this.md = false;
            }
    
            @HostListener('document:mousemove', ['$event'])
            onMouseMove(event:MouseEvent) {
              if(this.md && this._allowDrag){
                this.element.nativeElement.style.top = (event.clientY - this.topStart) + 'px';
                this.element.nativeElement.style.left = (event.clientX - this.leftStart) + 'px';
              }
            }
    
            @HostListener('touchstart', ['$event'])
            onTouchStart(event:TouchEvent) {
              this.md = true;
              this.topStart = event.changedTouches[0].clientY - this.element.nativeElement.style.top.replace('px','');
              this.leftStart = event.changedTouches[0].clientX - this.element.nativeElement.style.left.replace('px','');
              event.stopPropagation();
            }
    
            @HostListener('document:touchend')
            onTouchEnd() {
              this.md = false;
            }
    
            @HostListener('document:touchmove', ['$event'])
            onTouchMove(event:TouchEvent) {
              if(this.md && this._allowDrag){
                this.element.nativeElement.style.top = ( event.changedTouches[0].clientY - this.topStart ) + 'px';
                this.element.nativeElement.style.left = ( event.changedTouches[0].clientX - this.leftStart ) + 'px';
              }
              event.stopPropagation();
            }
    
            @Input('ng2-draggable')
            set allowDrag(value:boolean){
              this._allowDrag = value;
              if(this._allowDrag)
                this.element.nativeElement.className += ' cursor-draggable';
              else
                this.element.nativeElement.className = this.element.nativeElement.className
                                                        .replace(' cursor-draggable','');
            }
    }
    

提交回复
热议问题