yur*_*zui 46
你可以简单地使用cdkDrag指令@angular/cdk/drag-drop
dialog.html
<h1 mat-dialog-title
cdkDrag
cdkDragRootElement=".cdk-overlay-pane"
cdkDragHandle>
Hi {{data.name}}
</h1>
Run Code Online (Sandbox Code Playgroud)
由于没有官方解决方案,我将编写将应用于对话框标题的自定义指令,并为我们完成所有工作:
dialog.html
@Component({
selector: 'app-simple-dialog',
template: `
<h1 mat-dialog-title mat-dialog-draggable-title>Hi {{data.name}}</h1>
^^^^^^^^^^^^^^^^^^^^^^^^^^^
<div mat-dialog-content>
...
</div>
<div mat-dialog-actions>
...
</div>
`
})
export class SimpleDialogComponent {
Run Code Online (Sandbox Code Playgroud)
这里的基本思想是使用MatDialogRef.updatePosition方法更新对话框位置.在引擎盖下,这种方法改变了边缘 - 边缘 - 左边的值,有人可以说这不是最好的选择,如果我们使用变换会更好但我只是想展示一个如何在没有一些的情况下做到这一点的例子技巧和内置服务的帮助.
我们还需要在我们的指令中注入MatDialogContainer,以便我们可以获得对话框容器的初始位置.我们必须计算初始偏移量,因为Angular材质库使用flex到center对话框,它不会获得特定的top/left值.
对话框的拖动,title.directive.ts
import { Directive, HostListener, OnInit } from '@angular/core';
import { MatDialogContainer, MatDialogRef } from '@angular/material';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from 'rxjs/Observable';
import { takeUntil } from 'rxjs/operators/takeUntil';
import 'rxjs/add/observable/fromEvent';
import { take } from 'rxjs/operators/take';
@Directive({
selector: '[mat-dialog-draggable-title]'
})
export class DialogDraggableTitleDirective implements OnInit {
private _subscription: Subscription;
mouseStart: Position;
mouseDelta: Position;
offset: Position;
constructor(
private matDialogRef: MatDialogRef<any>,
private container: MatDialogContainer) {}
ngOnInit() {
this.offset = this._getOffset();
}
@HostListener('mousedown', ['$event'])
onMouseDown(event: MouseEvent) {
this.mouseStart = {x: event.pageX, y: event.pageY};
const mouseup$ = Observable.fromEvent(document, 'mouseup');
this._subscription = mouseup$.subscribe(() => this.onMouseup());
const mousemove$ = Observable.fromEvent(document, 'mousemove')
.pipe(takeUntil(mouseup$))
.subscribe((e: MouseEvent) => this.onMouseMove(e));
this._subscription.add(mousemove$);
}
onMouseMove(event: MouseEvent) {
this.mouseDelta = {x: (event.pageX - this.mouseStart.x), y: (event.pageY - this.mouseStart.y)};
this._updatePosition(this.offset.y + this.mouseDelta.y, this.offset.x + this.mouseDelta.x);
}
onMouseup() {
if (this._subscription) {
this._subscription.unsubscribe();
this._subscription = undefined;
}
if (this.mouseDelta) {
this.offset.x += this.mouseDelta.x;
this.offset.y += this.mouseDelta.y;
}
}
private _updatePosition(top: number, left: number) {
this.matDialogRef.updatePosition({
top: top + 'px',
left: left + 'px'
});
}
private _getOffset(): Position {
const box = this.container['_elementRef'].nativeElement.getBoundingClientRect();
return {
x: box.left + pageXOffset,
y: box.top + pageYOffset
};
}
}
export interface Position {
x: number;
y: number;
}
Run Code Online (Sandbox Code Playgroud)
自@Rolando问:
我想"记住"模态所在的位置,这样当按下打开模态的按钮时,模态会打开"它最后定位"的位置.
让我们试着支持它.
为此,您可以创建一些存储对话位置的服务:
莫代尔,position.cache.ts
@Injectable()
export class ModalPositionCache {
private _cache = new Map<Type<any>, Position>();
set(dialog: Type<any>, position: Position) {
this._cache.set(dialog, position);
}
get(dialog: Type<any>): Position|null {
return this._cache.get(dialog);
}
}
Run Code Online (Sandbox Code Playgroud)
现在你需要在我们的指令中注入这个服务:
对话框的拖动,title.directive.ts
export class DialogDraggableTitleDirective implements OnInit {
...
constructor(
private matDialogRef: MatDialogRef<any>,
private container: MatDialogContainer,
private positionCache: ModalPositionCache
) {}
ngOnInit() {
const dialogType = this.matDialogRef.componentInstance.constructor;
const cachedValue = this.positionCache.get(dialogType);
this.offset = cachedValue || this._getOffset();
this._updatePosition(this.offset.y, this.offset.x);
this.matDialogRef.beforeClose().pipe(take(1))
.subscribe(() => this.positionCache.set(dialogType, this.offset));
}
Run Code Online (Sandbox Code Playgroud)
一旦对话框关闭,我就可以保存最后的偏移量.
这种方式对话框会记住它被关闭的位置
在您的模块中,导入 cdk 拖动
import { DragDropModule } from '@angular/cdk/drag-drop';
Run Code Online (Sandbox Code Playgroud)
例如,在对话框所在的 html 中,只需添加到任何 html 元素即可。我已添加到第一个元素,然后我可以将对话框拖动到我选择的任何位置。
<mat-dialog-content cdkDrag cdkDragRootElement=".cdk-overlay-pane" cdkDragHandle>
content...
</mat-dialog-content>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8220 次 |
| 最近记录: |