简体   繁体   English

Angular 材质 CDK 拖放 对齐网格内部元素 cdkDragConstrainPosition @Input 用于拖放元素内的元素 position

[英]Angular Material CDK Drag and Drop Snap To Grid Internal Element cdkDragConstrainPosition @Input for element inside the Drag and Drop element position

When using Drag and Drop via Angular Material CDK, it allows you to "snap to grid" via [cdkDragConstrainPosition]="computedDragRenderPos"通过 Angular 材质 CDK 使用拖放时,它允许您通过[cdkDragConstrainPosition]="computedDragRenderPos"

Example:例子:

<div cdkDrag [cdkDragConstrainPosition]="computeDragRenderPos">
  ...
</div>
export class MyDraggableComponent {

  constructor() {}

  computeDragRenderPos(pos, dragRef) {
    return {x: Math.floor(pos.x / 30) * 30, y: pos.y}; // will render the element every 30 pixels horizontally
  }
}

However, that passes in pos which is the mouse pos.但是,这会传入 pos,即鼠标 pos。 I understand you can pass dragRef and get the element's getBoundingClientRect to get the x but as soon as you pass that into the cdkDragConstrainPosition position and change the position of the element it no longer works.我知道你可以通过 dragRef 并获取元素的 getBoundingClientRect 来获取 x 但是一旦你将它传递到cdkDragConstrainPosition position 并更改元素的 position 它不再起作用。

How do you move an internal element every 30px.... and not the position of the start of the element?你如何每 30px 移动一个内部元素......而不是元素开头的 position?

i have close issue我有一个关闭的问题

dragRef it is a ref to dragable element if you want snap to some grid you need to calculate position depend on other element dragRef 它是可拖动元素的引用,如果你想捕捉到某个网格,你需要计算 position 取决于其他元素

@ViewChild("someElement") someElement;

computeDragRenderPos(pos, dragRef) {
    return {
       x: this.someElement.nativeElement.getBoundingClientRect().x,
       y: this.someElement.nativeElement.getBoundingClientRect().y };
  }

you need to include some cursor logic here to keep it work您需要在此处包含一些 cursor 逻辑以使其正常工作

in my case it is also lost scope of the component class for some reason (not sure it was personal problem or global) i solve it with bind(this) before pass function to cdkDragConstrainPosition在我的情况下,由于某种原因(不确定这是个人问题还是全局问题),它也丢失了组件 class 的 scope (不确定这是个人问题还是全局问题)我在将 functionZ 传递给cdkDragConstrainPosition之前使用bind(this)解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM