Skip to content

Commit 1f606ca

Browse files
committed
chore: test preview
1 parent 493cd02 commit 1f606ca

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/image-viewer/_example/align.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function AlignDemo() {
2525
基础图片预览 + 对齐方式
2626
</Button>
2727

28-
<ImageViewer images={images} visible={visible} onClose={() => setVisible(false)} />
28+
<ImageViewer images={images} visible={visible} maxZoom={10} onClose={() => setVisible(false)} />
2929
</div>
3030
);
3131
}

src/image-viewer/image-viewer.tsx

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,71 @@ const ImageViewer: React.FC<ImageViewerProps> = (props) => {
128128
exitActive: 'fade-leave-active',
129129
};
130130

131+
const getMaxDraggedX = () => {
132+
const rootOffsetWidth = rootRef.current?.offsetWidth || 0;
133+
const scaledWidth = transform.scale * rootOffsetWidth;
134+
return Math.max(0, (scaledWidth - rootOffsetWidth) / 2);
135+
};
136+
137+
const getMaxDraggedY = (index: number) => {
138+
const rootOffsetHeight = rootRef.current?.offsetHeight || 0;
139+
const currentImageHeight = imgRefs.current[currentIndex]?.offsetHeight;
140+
if (!currentImageHeight || !rootOffsetHeight) {
141+
return {
142+
top: 0,
143+
bottom: 0,
144+
};
145+
}
146+
const currentImageScaledHeight = transform.scale * currentImageHeight;
147+
const halfScaleHeight = (currentImageScaledHeight - currentImageHeight) / 2;
148+
if (currentImageScaledHeight <= rootOffsetHeight) {
149+
return {
150+
top: 0,
151+
bottom: 0,
152+
};
153+
}
154+
const diffHeight = currentImageScaledHeight - rootOffsetHeight;
155+
const centerDraggedY = diffHeight / 2;
156+
const alignmentDraggedY = {
157+
start: {
158+
top: -diffHeight + halfScaleHeight,
159+
bottom: halfScaleHeight,
160+
},
161+
center: {
162+
top: -centerDraggedY,
163+
bottom: centerDraggedY,
164+
},
165+
end: {
166+
top: -halfScaleHeight,
167+
bottom: diffHeight - halfScaleHeight,
168+
},
169+
};
170+
const alignment = imageInfoList[index]?.image?.align || 'center';
171+
return alignmentDraggedY[alignment];
172+
};
173+
174+
const getRealTransformX = () => {
175+
const max = getMaxDraggedX();
176+
if (transform.x < -max) {
177+
return -max;
178+
}
179+
if (transform.x > max) {
180+
return max;
181+
}
182+
return transform.x;
183+
};
184+
185+
const getRealTransformY = (index: number) => {
186+
const { top, bottom } = getMaxDraggedY(index);
187+
if (transform.y <= 0 && transform.y < top) {
188+
return top;
189+
}
190+
if (transform.y >= 0 && transform.y > bottom) {
191+
return bottom;
192+
}
193+
return transform.y;
194+
};
195+
131196
return (
132197
<CSSTransition
133198
in={show}
@@ -168,7 +233,7 @@ const ImageViewer: React.FC<ImageViewerProps> = (props) => {
168233
imgRefs.current[index] = node;
169234
}}
170235
style={{
171-
transform: `matrix(${transform.scale}, 0, 0, ${transform.scale}, ${transform.x}, 0)`,
236+
transform: `matrix(${transform.scale}, 0, 0, ${transform.scale}, ${getRealTransformX()}, ${getRealTransformY(index)})`,
172237
transitionDuration: isTouching ? '0s' : '.3s',
173238
}}
174239
className={`${imageViewerClass}__img`}

0 commit comments

Comments
 (0)