gpt4 book ai didi

drag-and-drop - CDK拖放自动滚动

转载 作者:行者123 更新时间:2023-12-03 17:14:00 26 4
gpt4 key购买 nike

我有一个 Angular 7 应用程序,使用 CDK Drag-n-Drop 在一个很长的列表中拖放行。

我应该怎么做才能让长列表在拖出当前 View 时自动滚动?

我可以引用任何示例代码吗?

最佳答案

我遇到了同样的问题,它发生在外部元素可滚动的任何时候。这是 Unresolved 问题 - https://github.com/angular/components/issues/16677 . - 我稍微修改了此链接中提到的解决方案。

import { Directive, Input, ElementRef, AfterViewInit } from '@angular/core';
import { CdkDrag } from '@angular/cdk/drag-drop';

@Directive({
selector: '[cdkDrag],[actualContainer]',
})
export class CdkDropListActualContainerDirective {
@Input('actualContainer') actualContainer: string;
originalElement: ElementRef<HTMLElement>;

constructor(cdkDrag: CdkDrag) {
cdkDrag._dragRef.beforeStarted.subscribe( () => {
var cdkDropList = cdkDrag.dropContainer;
if (!this.originalElement) {
this.originalElement = cdkDropList.element;
}

if ( this.actualContainer ) {
const element = this.originalElement.nativeElement.closest(this.actualContainer) as HTMLElement;
cdkDropList._dropListRef.element = element;
cdkDropList.element = new ElementRef<HTMLElement>(element);
} else {
cdkDropList._dropListRef.element = cdkDropList.element.nativeElement;
cdkDropList.element = this.originalElement;
}
});
}
}
模板
 <div mat-dialog-content class="column-list">
<div class="column-selector__list">
<div cdkDropList (cdkDropListDropped)="drop($event)">
<div
*ngFor="let column of data"
cdkDrag
actualContainer="div.column-list"
>
</div>
</div>
</div>
</div>

关于drag-and-drop - CDK拖放自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55821076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com