拖动时如何更改光标?材料 CDK 拖放

Pat*_*ick 6 css drag-and-drop angular-material

使用 Material CDK 库中的拖放行为,我尝试在拖动cdkDrag元素时更改光标。

例如,在此 StackBlitz 中,光标位于grab悬停时。我希望它grabbing在拖动时更改为。这方面的一个例子是在 Google 表格中抓取一行时发生的情况:

在此处输入图片说明

阅读有关拖放组件样式的文档,看起来向此类添加光标属性应该可以解决问题:

.cdk-drop-list-dragging:在用户拖动项目时添加到 cdkDropList 的类。

代码如下所示:

.example-box {
  /* other CSS properties */
  cursor: grab;
}

.cdk-drop-list-dragging {
  cursor: grabbing;
}
Run Code Online (Sandbox Code Playgroud)

但是,正如您在 StackBlitz 中看到的那样,这似乎并没有改变光标。我猜这是因为这个类适用于列表而不是光标。

另一个潜力是.cdk-drag-preview课程:

.cdk-drag-preview:这是在用户拖动可排序列表中的项目时将呈现在用户光标旁边的元素。默认情况下,元素看起来与被拖动的元素完全一样。

这似乎也不起作用。我认为这是因为它更改了光标旁边呈现的元素,而不是光标本身。

关于如何在拖动时更改光标的任何想法?

Nev*_*veh 5

以前的解决方案对我不起作用,但这里有一些很可能适用于仍然有问题的人:

首先添加这个全局 CSS:

body.inheritCursors * {
  cursor: inherit !important;
}
Run Code Online (Sandbox Code Playgroud)

并在您的 cdkDrag 元素中添加 cdkDragStarted 并将其附加到 .ts 文件中的方法:

<div cdkDrag (cdkDragStarted)="dragStart($event)"></div>
Run Code Online (Sandbox Code Playgroud)

在您的 .ts 文件中,您可以在拖动开始和停止时切换所需的光标:

bodyElement: HTMLElement = document.body;

  dragStart(event: CdkDragStart) {
    this.bodyElement.classList.add('inheritCursors');
    this.bodyElement.style.cursor = 'move'; 
    //replace 'move' with what ever type of cursor you want
  }

  drop(event: CdkDragDrop<string[]>) {
    this.bodyElement.classList.remove('inheritCursors');
    this.bodyElement.style.cursor = 'unset';
    ...
    ...
  }
Run Code Online (Sandbox Code Playgroud)

这是StackBlitz 上一个工作示例的链接

希望这是有帮助的。


Mar*_*hal 1

只需添加cursor: grabbing到您的example-box:active

.example-box:active {
  box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
              0 8px 10px 1px rgba(0, 0, 0, 0.14),
              0 3px 14px 2px rgba(0, 0, 0, 0.12);
  cursor: grabbing;
}
Run Code Online (Sandbox Code Playgroud)

:active 选择器用于选择活动链接并设置其样式。

单击链接后,该链接将变为活动状态。

提示:: active 选择器可用于所有元素,而不仅仅是链接。

此处提供更多信息

https://www.w3schools.com/cssref/sel_active.asp


斯塔克闪电战

https://stackblitz.com/edit/angular-b8kjj3-r993mc?embed=1&file=app/cdk-drag-drop-overview-example.css