@@ -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