diff --git a/components/image/image-preview.component.ts b/components/image/image-preview.component.ts
index 9973a77fbe5..8b4dc203e8f 100644
--- a/components/image/image-preview.component.ts
+++ b/components/image/image-preview.component.ts
@@ -63,10 +63,10 @@ const initialPosition = {
@@ -176,6 +176,7 @@ export class NzImagePreviewComponent implements OnInit {
closeClick = new EventEmitter();
@ViewChild('imgRef') imageRef!: ElementRef;
+ @ViewChild('imagePreviewWrapper', { static: true }) imagePreviewWrapper!: ElementRef;
private zoom: number;
private rotate: number;
@@ -214,6 +215,12 @@ export class NzImagePreviewComponent implements OnInit {
this.ngZone.run(() => this.containerClick.emit());
}
});
+
+ fromEvent(this.imagePreviewWrapper.nativeElement, 'mousedown')
+ .pipe(takeUntil(this.destroy$))
+ .subscribe(() => {
+ this.isDragging = true;
+ });
});
}
@@ -319,10 +326,6 @@ export class NzImagePreviewComponent implements OnInit {
this.cdr.markForCheck();
}
- onDragStarted(): void {
- this.isDragging = true;
- }
-
onDragReleased(): void {
this.isDragging = false;
const width = this.imageRef.nativeElement.offsetWidth * this.zoom;
diff --git a/components/image/image.spec.ts b/components/image/image.spec.ts
index 8f2852fa7ea..b27616970ae 100644
--- a/components/image/image.spec.ts
+++ b/components/image/image.spec.ts
@@ -443,11 +443,12 @@ describe('Preview', () => {
it('should drag released work', fakeAsync(() => {
context.images = [{ src: QUICK_SRC }];
context.createUsingService();
- const previewInstance = context.previewRef?.previewInstance;
+ const previewInstance = context.previewRef?.previewInstance!;
tickChanges();
- previewInstance?.onDragStarted();
- previewInstance?.onDragReleased();
- expect(previewInstance?.position).toEqual({ x: 0, y: 0 });
+ previewInstance.imagePreviewWrapper.nativeElement.dispatchEvent(new MouseEvent('mousedown'));
+ expect(previewInstance.isDragging).toEqual(true);
+ previewInstance.onDragReleased();
+ expect(previewInstance.position).toEqual({ x: 0, y: 0 });
}));
it('should position calculate correct', () => {