【问题标题】:How can I change the cursor when dragging? Material CDK Drag and Drop拖动时如何更改光标?材质 CDK 拖放
【发布时间】:2019-05-09 12:18:17
【问题描述】:

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

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

阅读 the documentation 以设置拖放组件的样式,看起来向此类添加光标属性应该可以解决问题:

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

代码如下所示:

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

.cdk-drop-list-dragging {
  cursor: grabbing;
}

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

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

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

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

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

【问题讨论】:

标签: css drag-and-drop angular-material


【解决方案1】:

以前的解决方案对我不起作用,但对于仍有问题的任何人来说,这很可能适用:

首先添加这个全局 CSS:

body.inheritCursors * {
  cursor: inherit !important;
}

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

<div cdkDrag (cdkDragStarted)="dragStart($event)"></div>

在您的 .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';
    ...
    ...
  }

这是一个工作example on StackBlitz的链接

希望这对您有所帮助。

【讨论】:

  • 标记的答案适用于 OP 的示例。然而,这个答案在更一般的意义上有效,特别是在使用 cdkDropList 时。
  • 是的,我相信对于那些正在与 OP 遇到类似障碍并在这里寻找解决方案的人来说,这是一个不错的选择 - 就我而言,我有多个 cdkDropList's我连接的。将 cdkDrag 元素拖出其原始父元素时,我没有得到想要的抓取光标。
【解决方案2】:

只需将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;
}

: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

【讨论】:

  • 您能否在答案中添加active 在这里做了什么以及您如何知道使用该属性?我问是因为我在文档中没有看到任何解释,我不仅想知道答案,还想知道背后的原因。
  • 请随时解释任何不赞成票的原因
  • @Marshal 知道如何使它适用于 cdkDropList 吗?此示例不起作用。
【解决方案3】:

对于我自己,我添加了以下样式覆盖以在拖动时重新启用自定义光标。

.draggable-element .drag-handle{
  cursor: grab;
}

.draggable-element.cdk-drag-preview .drag-handle{
  pointer-events: auto;
  cursor: grabbing;
}

链接到live Example

【讨论】:

  • 嗨乔尔,感谢您的贡献。不幸的是,这个解决方案并没有像接受的答案那样复制点击时的抓取行为(因为它只在拖动时抓取)。
【解决方案4】:

只需使用onmousedown = "changeCursorPoint()"事件函数-

private changeCursorPoint(): void {
    document.body.style.cursor = 'grabbing';
}

(cdkDropListDropped) = "clearCursorEvent()"上再次清除函数

private changeCursorToDefault(): void {
    document.body.style.cursor = 'default';
 }

【讨论】:

    【解决方案5】:

    在您链接的 Stackblitz 示例中,您没有使用下拉列表,因此您希望您的 css 是:

    .cdk-drag-dragging {
      cursor: grabbing;
    }
    

    在我的例子中,我使用 table-body 元素上的列表进行拖放:

    table tbody.cdk-drop-list-dragging td {
      cursor: grabbing !important;
    }
    

    【讨论】:

    • 接受的答案使用 OP 引用的链接 stackblitz 来提供问题的解决方案。
    • 确实,使用下拉列表时接受的答案是不够的。一旦您开始在元素周围移动,它将失去样式。但这个答案也不是很完整,因为它会错过一开始的抓取(在开始移动之前)。为了使其充分发挥作用,我必须将这个答案和接受的答案结合起来。
    猜你喜欢
    • 1970-01-01
    • 2012-04-24
    • 2021-07-24
    • 2017-06-09
    • 2012-10-02
    • 2021-03-25
    • 2018-12-01
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多