我正在使用 Angular 和 Material 创建一个 Web 应用程序,我希望能够拖放和调整我的部分的大小。有没有办法使用角材料拖放cdk来完成?
问问题
2197 次
2 回答
1
如果您正在寻找"non-third-party dependent"
解决方案,那么以下是最好的。
以下已针对Angular 9进行了测试。
民间版本(同上,以防原件被删除)
HTML
<div #resizeBox class="container" style="position: relative;" [style.width.px]="width"
[style.height.px]="height">
<span #dragHandleCorner [cdkDragLockAxis]="lockAxis" class="dragHandle corner" cdkDrag (cdkDragMoved)="dragMove(dragHandleCorner, $event)"></span>
<span #dragHandleRight cdkDragLockAxis="x" class="dragHandle right" cdkDrag (cdkDragMoved)="dragMove(dragHandleRight, $event)"></span>
<span #dragHandleBottom cdkDragLockAxis="y" class="dragHandle bottom" cdkDrag (cdkDragMoved)="dragMove(dragHandleBottom, $event)"></span>
<span class="full" style="width: 100%;">Hello World!</span>
</div>
组件.ts
import { CdkDragMove } from '@angular/cdk/drag-drop';
import { AfterViewInit, Component, ElementRef, NgZone, ViewChild } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements AfterViewInit {
@ViewChild('resizeBox') resizeBox: ElementRef;
@ViewChild('dragHandleCorner') dragHandleCorner: ElementRef;
@ViewChild('dragHandleRight') dragHandleRight: ElementRef;
@ViewChild('dragHandleBottom') dragHandleBottom: ElementRef;
get resizeBoxElement(): HTMLElement {
return this.resizeBox.nativeElement;
}
get dragHandleCornerElement(): HTMLElement {
return this.dragHandleCorner.nativeElement;
}
get dragHandleRightElement(): HTMLElement {
return this.dragHandleRight.nativeElement;
}
get dragHandleBottomElement(): HTMLElement {
return this.dragHandleBottom.nativeElement;
}
constructor(private ngZone: NgZone) {}
ngAfterViewInit() {
this.setAllHandleTransform();
}
setAllHandleTransform() {
const rect = this.resizeBoxElement.getBoundingClientRect();
this.setHandleTransform(this.dragHandleCornerElement, rect, 'both');
this.setHandleTransform(this.dragHandleRightElement, rect, 'x');
this.setHandleTransform(this.dragHandleBottomElement, rect, 'y');
}
setHandleTransform(
dragHandle: HTMLElement,
targetRect: ClientRect | DOMRect,
position: 'x' | 'y' | 'both'
) {
const dragRect = dragHandle.getBoundingClientRect();
const translateX = targetRect.width - dragRect.width;
const translateY = targetRect.height - dragRect.height;
if (position === 'x') {
dragHandle.style.transform = `translate3d(${translateX}px, 0, 0)`;
}
if (position === 'y') {
dragHandle.style.transform = `translate3d(0, ${translateY}px, 0)`;
}
if (position === 'both') {
dragHandle.style.transform = `translate3d(${translateX}px, ${translateY}px, 0)`;
}
}
dragMove(dragHandle: HTMLElement, $event: CdkDragMove<any>) {
this.ngZone.runOutsideAngular(() => {
this.resize(dragHandle, this.resizeBoxElement);
});
}
resize(dragHandle: HTMLElement, target: HTMLElement) {
const dragRect = dragHandle.getBoundingClientRect();
const targetRect = target.getBoundingClientRect();
const width = dragRect.left - targetRect.left + dragRect.width;
const height = dragRect.top - targetRect.top + dragRect.height;
target.style.width = width + 'px';
target.style.height = height + 'px';
this.setAllHandleTransform();
}
}
CSS
.container {
position: relative;
background-color: gray;
}
.full {
width: 100%;
background: yellow;
}
.dragHandle {
position: absolute;
background-color: red;
}
.dragHandle.corner {
width: 15px;
height: 15px;
cursor: nwse-resize;
}
.dragHandle.right {
width: 2px;
height: 100%;
cursor: ew-resize;
}
.dragHandle.bottom {
height: 2px;
width: 100%;
cursor: ns-resize;
}
希望这对某人有帮助:)
谢谢。
于 2020-08-01T21:05:23.770 回答
0
对于调整窗口大小:(JQlite)
angular.element($window).triggerHandler('resize');
于 2019-06-25T09:38:23.010 回答