ng-zorro 弹窗可以拖动
import { Directive, ElementRef, Renderer2, AfterViewInit } from '@angular/core';@Directive({ selector: '[DragModal]'})export class DragModalDirective implements AfterViewInit { private canMove: boolean = false; private modalX: number = 0; private modalY: number = 0; private distX: number = 0; private distY: number = 0; constructor(private elementRef: ElementRef, private render: Renderer2) { } target: any = null; ngAfterViewInit() { var that: any = this; that.target = document.getElementsByClassName('dg_class'); let modalElement = that.getModalElement(); let modalTitleElement = that.getModalTitleElment(); that.render.listen(modalTitleElement, 'mousedown', function (event: any) { that.modalX = modalElement.offsetLeft; that.modalY = modalElement.offsetTop; that.distX = event.clientX - modalElement.offsetLeft; that.distY = event.clientY - modalElement.offsetTop; that.render.setStyle(modalElement, "position", "absolute"); that.render.setStyle(modalElement, "top", `${that.modalY}px`); that.render.setStyle(modalElement, "left", `${that.modalX}px`); that.canMove = true; }.bind(that)); that.render.listen(modalTitleElement, 'mouseup', function (event: any) { that.canMove = false; }.bind(that)); that.render.listen(that.target[that.target.length - 1], 'mousemove', function (event: any) { if (that.canMove) { let moveX = event.clientX - that.distX; let moveY = event.clientY - that.distY; const modalWidth = modalElement.offsetWidth; const modalHeight = modalElement.offsetHeight; const cw = document.documentElement.clientWidth; const cy = document.documentElement.clientHeight; if (moveY < 0) { moveY = 0; } else if (moveY > cy - modalHeight) { moveY = cy - modalHeight; } if (moveX < 0) { moveX = 0; } else if (moveX > cw - modalWidth) { moveX = cw - modalWidth; } that.render.setStyle(modalElement, "top", `${moveY}px`); that.render.setStyle(modalElement, "left", `${moveX}px`); // that.render.setStyle(modalTitleElement, "cursor", `move`); } }.bind(that)); } getModalElement() { return this.target[this.target.length - 1].querySelector('.ant-modal'); } getModalTitleElment() { return this.target[this.target.length - 1].querySelector('.ant-modal-header'); }}