提问者:小点点

如何自定义角材质7中的CSS拖拽,拖动预览和拖动占位符?


我找到了一些如何使用. cdk-draad-preview.cdk-draad-locehold类的基本示例,它们似乎在没有嵌套元素的情况下可以完成这项工作。

基本上,我有一个动作列表,每个动作都以复杂的mat-card格式表示。这部分实际上是作为另一个组件完成的,但为了这个例子,我将尽我所能使它尽可能基本。

我的例子类似于这个结构:

<style>
    .my_action { border: 2px solid red; }
</style>

<div class="drop_area" cdkDropList>
    <div *ngFor="let action of actions"
        (cdkDragStarted)="dragStart($event, action)"
        (cdkDragMoved)="dragMoved($event, action)"
        (cdkDragEnded)="dragEnded($event, action)" cdkDrag>

        <mat-card class="my_action">
            {{ action.name }}
        </mat-card>

    </div>
</div>

在角分量

dragStart(e, action) { 
    // initialize start X coord
    this.startX = 0;

    // initialize start Y coord
    this.startY = 0;
}

dragMoved(e, action) {
    // record new position
    this.endX = e.pointerPosition.x;
    this.endY = e.pointerPosition.y;

    // logic to set startX and startY
    // TRYING TO CHANGE CARD BORDER COLOR IF this.endX - this.startX > some number
}

我希望能够做到以下几点:

  • 向左拖动卡片时,将其边框颜色更改为蓝色,向右拖动时更改为绿色
  • 相应地更改拖动占位符的边框颜色。

使用. cdk-drad-preview.cdk-drad-locehold的问题似乎适用于可拖动的divmat-card的父级);然而,我正在尝试更改其子边框的颜色。

提前感谢:)

附录

我最重要的挑战是更改拖动占位符中的边框颜色。我正在尝试更改占位符的边框颜色和左边距,以指示用户操作已向左或向右移动,代表列表中的不同级别。


共3个答案

匿名用户

下面这样的事情会实现你的目标吗?

初始化nativeElementx和y

dragStart(e, action) {
    const rect = e.source.element.nativeElement.getBoundingClientRect();

    // initialize start X coord
    this.startX = rect.x;
    // initialize start Y coord
    this.startY = rect.y;
  }

比较X偏移量并使用rendere2nativeElement上设置样式

dragMoved(e, action) {
    // record new position
    this.currentX = e.event.clientX;
    this.currentY = e.event.clientY;
    // logic to set startX and startY
    // TRYING TO CHANGE CARD BORDER COLOR IF this.endX - this.startX > some number
    if(this.startX < this.currentX){
      this._renderer.setStyle(e.source.element.nativeElement, 'border-style', 'solid');
      this._renderer.setStyle(e.source.element.nativeElement, 'border-color', 'green');
    }
    else if (this.startX > this.currentX){
      this._renderer.setStyle(e.source.element.nativeElement, 'border-style', 'solid');
      this._renderer.setStyle(e.source.element.nativeElement, 'border-color', 'blue');
    }
  }

修订:

要在拖动时更改颜色,请执行以下操作。

获取视图中对#cdkDropList的引用。

@ViewChild('cdkDropList') _dropList:any;

*ngFor中设置索引

*ngFor="let action of actions; let i = index"

将索引传递给函数

(cdkDragMoved)="dragMoved($event, action, i)"

接收索引并进入cdkDropList的子级以设置样式。

dragMoved(e, action, i) {
    // record new position
    this.currentX = e.event.clientX;
    this.currentY = e.event.clientY;
    // logic to set startX and startY
    // TRYING TO CHANGE CARD BORDER COLOR IF this.endX - this.startX > some number
    if(this.startX < this.currentX){
      this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-style', 'solid');
      this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-color', 'green');
    }
    else if (this.startX > this.currentX){
      this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-style', 'solid');
       this._renderer.setStyle(this._dropList.nativeElement.children[i], 'border-color', 'blue');
    }
  }

Stackblitz

https://stackblitz.com/edit/angular-hdskvc?embed=1

匿名用户

当一个cdkDrag元素被拾取时,它将创建一个在拖动时可见的预览元素。这将是原始元素的克隆。克隆的元素将删除其id属性并添加类. cdk-drad-preview。

谨慎使用cdkDragMoked,因为此事件将为用户拖动的每个像素触发。

<div cdkDropList class="example-list">
    <div class="example-box" *ngFor="let action of actions" cdkDrag 
        (cdkDragStarted)="dragStart($event)"
        (cdkDragMoved)="dragMoved($event)">
        {{ action.name }}
    </div>
</div>
export class CustomComponent {
    cloned: any;

    actions = [
        { name: "one" },
        { name: "two" },
        { name: "three" }
    ];

    constructor(private _renderer: Renderer2) { }

    dragStart(event) {
        this.cloned = document.getElementsByClassName("cdk-drag-preview")[0];
    }

    dragMoved(event) {
        this._renderer.removeClass(this.cloned, "red");
        this._renderer.removeClass(this.cloned, "green");

        const distance: { x: number, y: number } = event.distance;
        this._renderer.addClass(this.cloned, (distance.x > 0) ? "green" : "red");
    }
}

匿名用户

你可以这样做:

<div class="drop_area" cdkDropList>
<div *ngFor="let action of actions;let i = index;"
    (cdkDragStarted)="dragStart($event, action)"
    (cdkDragMoved)="dragMoved($event, action, i)"
    (cdkDragEnded)="dragEnded($event, action)" cdkDrag>

    <mat-card class="my_action{{i}}">
        {{ action.name }}
    </mat-card>

</div>
dragMoved(e, action, index: number) {
    // record new position
    this.endX = e.pointerPosition.x;
    this.endY = e.pointerPosition.y;

    // login to set startX and startY
    // TRYING TO CHANGE CARD BORDER COLOR IF this.endX - this.startX > some number

   document.getElementsByClassName('my_action'+index)[0].style.borderColor='blue';
}

相关问题