angular-ng-zorro,自定义模态窗拖动指令

Posted huangmin1992

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了angular-ng-zorro,自定义模态窗拖动指令相关的知识,希望对你有一定的参考价值。

技术图片
import  Directive, ElementRef, Renderer2, AfterViewInit  from ‘@angular/core‘;
@Directive(
  selector: ‘[appDragModal]‘
)
export class DragModalDirective implements AfterViewInit 
  private canMove: boolean = false;
  private modalX: number = 0;
  private modalY: number = 0;
  private mouseDownX: number = 0;
  private mouseDownY: number = 0;
  private distX: number = 0;
  private distY: number = 0;
  constructor(private elementRef: ElementRef, private render: Renderer2) 
  
  ngAfterViewInit() 
    let modalElement = this.getModalElement();
    let modalTitleElement = this.getModalTitleElment();
    console.log(modalElement)
    console.log(modalTitleElement);
    this.render.listen(modalTitleElement, ‘mousedown‘, function (event) 
      this.mouseDownX = event.clientX;
      this.mouseDownY = event.clientY;
      this.modalX = modalElement.offsetLeft;
      this.modalY = modalElement.offsetTop;
      this.distX = event.clientX - modalElement.offsetLeft;
      this.distY = event.clientY - modalElement.offsetTop;
      this.render.setStyle(modalElement, "position", "absolute");
      this.render.setStyle(modalElement, "top", `$this.modalYpx`);
      this.render.setStyle(modalElement, "left", `$this.modalXpx`);
      this.canMove = true;
    .bind(this));
    this.render.listen(modalTitleElement, ‘mouseup‘, function (event) 
      this.canMove = false;
    .bind(this));
    this.render.listen(this.elementRef.nativeElement, ‘mousemove‘, function (event) 
      if (this.canMove) 
        let moveX = event.clientX - this.distX;
        let moveY = event.clientY - this.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;
        

        this.render.setStyle(modalElement, "top", `$moveYpx`);
        this.render.setStyle(modalElement, "left", `$moveXpx`);
        this.render.setStyle(modalElement, "cursor", `move`);
      
    .bind(this));
  
  getModalElement() 
    return this.elementRef.nativeElement.querySelector(‘.ant-modal‘);
  
  getModalTitleElment() 
    console.log(this.elementRef.nativeElement)
    return this.elementRef.nativeElement.querySelector(‘.ant-modal-content‘);
  

View Code

 

以上是关于angular-ng-zorro,自定义模态窗拖动指令的主要内容,如果未能解决你的问题,请参考以下文章

vue给模态框弹窗添加路由,实现页面后退可以关闭模态框

uniapp 悬浮窗插件(在其他应用上层显示) Ba-FloatWindow

uniapp 悬浮窗插件(在其他应用上层显示) Ba-FloatWindow

微信小程序之自定义模态弹窗(带动画)实例

uniapp 悬浮窗(悬浮球动态菜单在其他应用上层显示) Ba-FloatBall

在 iPad 上具有自定义大小的 SwiftUI sheet() 模态