@@ -80,10 +80,32 @@ export default class ZoomPane {
80
80
// `percentageOffsetX` and `percentageOffsetY` must be percentages
81
81
// expressed as floats between `0' and `1`.
82
82
setPosition ( percentageOffsetX , percentageOffsetY , triggerRect ) {
83
- let left = - ( this . imgEl . clientWidth * percentageOffsetX - this . el . clientWidth / 2 ) ;
84
- let top = - ( this . imgEl . clientHeight * percentageOffsetY - this . el . clientHeight / 2 ) ;
85
- let maxLeft = - ( this . imgEl . clientWidth - this . el . clientWidth ) ;
86
- let maxTop = - ( this . imgEl . clientHeight - this . el . clientHeight ) ;
83
+ const { width : imgElWidth , height : imgElHeight } = this . imgEl . getBoundingClientRect ( ) ;
84
+ const { width : elWidth , height : elHeight } = this . el . getBoundingClientRect ( ) ;
85
+
86
+ const centreOfContainerX = elWidth / 2 ;
87
+ const centreOfContainerY = elHeight / 2 ;
88
+
89
+ const targetImgXToBeCentre = imgElWidth * percentageOffsetX ;
90
+ const targetImgYToBeCentre = imgElHeight * percentageOffsetY ;
91
+
92
+ let left = centreOfContainerX - targetImgXToBeCentre ;
93
+ let top = centreOfContainerY - targetImgYToBeCentre ;
94
+
95
+ const differenceBetweenContainerWidthAndImgWidth = elWidth - imgElWidth ;
96
+ const differenceBetweenContainerHeightAndImgHeight = elHeight - imgElHeight ;
97
+ const isContainerLargerThanImgX = differenceBetweenContainerWidthAndImgWidth > 0 ;
98
+ const isContainerLargerThanImgY = differenceBetweenContainerHeightAndImgHeight > 0 ;
99
+
100
+ const minLeft = isContainerLargerThanImgX ? differenceBetweenContainerWidthAndImgWidth / 2 : 0 ;
101
+ const minTop = isContainerLargerThanImgY ? differenceBetweenContainerHeightAndImgHeight / 2 : 0 ;
102
+
103
+ const maxLeft = isContainerLargerThanImgX
104
+ ? differenceBetweenContainerWidthAndImgWidth / 2
105
+ : differenceBetweenContainerWidthAndImgWidth ;
106
+ const maxTop = isContainerLargerThanImgY
107
+ ? differenceBetweenContainerHeightAndImgHeight / 2
108
+ : differenceBetweenContainerHeightAndImgHeight ;
87
109
88
110
if ( this . el . parentElement === this . settings . inlineContainer ) {
89
111
// This may be needed in the future to deal with browser event
@@ -94,31 +116,21 @@ export default class ZoomPane {
94
116
let scrollY = window . pageYOffset ;
95
117
96
118
let inlineLeft =
97
- triggerRect . left +
98
- percentageOffsetX * triggerRect . width -
99
- this . el . clientWidth / 2 +
100
- this . settings . inlineOffsetX +
101
- scrollX ;
119
+ triggerRect . left + percentageOffsetX * triggerRect . width - elWidth / 2 + this . settings . inlineOffsetX + scrollX ;
102
120
let inlineTop =
103
- triggerRect . top +
104
- percentageOffsetY * triggerRect . height -
105
- this . el . clientHeight / 2 +
106
- this . settings . inlineOffsetY +
107
- scrollY ;
121
+ triggerRect . top + percentageOffsetY * triggerRect . height - elHeight / 2 + this . settings . inlineOffsetY + scrollY ;
108
122
109
123
if ( this . settings . containInline ) {
110
- let elRect = this . el . getBoundingClientRect ( ) ;
111
-
112
124
if ( inlineLeft < triggerRect . left + scrollX ) {
113
125
inlineLeft = triggerRect . left + scrollX ;
114
- } else if ( inlineLeft + this . el . clientWidth > triggerRect . left + triggerRect . width + scrollX ) {
115
- inlineLeft = triggerRect . left + triggerRect . width - this . el . clientWidth + scrollX ;
126
+ } else if ( inlineLeft + elWidth > triggerRect . left + triggerRect . width + scrollX ) {
127
+ inlineLeft = triggerRect . left + triggerRect . width - elWidth + scrollX ;
116
128
}
117
129
118
130
if ( inlineTop < triggerRect . top + scrollY ) {
119
131
inlineTop = triggerRect . top + scrollY ;
120
- } else if ( inlineTop + this . el . clientHeight > triggerRect . top + triggerRect . height + scrollY ) {
121
- inlineTop = triggerRect . top + triggerRect . height - this . el . clientHeight + scrollY ;
132
+ } else if ( inlineTop + elHeight > triggerRect . top + triggerRect . height + scrollY ) {
133
+ inlineTop = triggerRect . top + triggerRect . height - elHeight + scrollY ;
122
134
}
123
135
}
124
136
@@ -127,14 +139,14 @@ export default class ZoomPane {
127
139
}
128
140
129
141
if ( ! this . settings . showWhitespaceAtEdges ) {
130
- if ( left > 0 ) {
131
- left = 0 ;
142
+ if ( left > minLeft ) {
143
+ left = minLeft ;
132
144
} else if ( left < maxLeft ) {
133
145
left = maxLeft ;
134
146
}
135
147
136
- if ( top > 0 ) {
137
- top = 0 ;
148
+ if ( top > minTop ) {
149
+ top = minTop ;
138
150
} else if ( top < maxTop ) {
139
151
top = maxTop ;
140
152
}
0 commit comments