@@ -131,17 +131,11 @@ document.addEventListener("DOMContentLoaded", function () {
131131 * @param {Config } config - The preview width will be the min of this and the window width.
132132 */
133133function setPreviewPosition ( preview , anchor , config ) {
134- // compute required link and window data
135- let position_anchor = {
136- left : anchor . offsetLeft - window . scrollX ,
137- top : anchor . offsetTop - window . scrollY ,
138- } ;
139-
140134 // set iframe position relative to link,
141135 // by default anchoring below and right of it
142- let pos_left = anchor . offsetLeft + config . offset . left ;
143- let pos_screen_left = position_anchor . left + config . offset . left ;
144- let pos_screen_top = position_anchor . top + config . offset . top ;
136+ let anchorViewPos = anchor . getBoundingClientRect ( ) ;
137+ let previewViewPosLeft = anchorViewPos . left + config . offset . left ;
138+ let previewViewPosTop = anchorViewPos . top + config . offset . top ;
145139 let width = config . width ;
146140 let height = config . height ;
147141 let marginX = 10 ;
@@ -152,34 +146,34 @@ function setPreviewPosition(preview, anchor, config) {
152146 width = maxWidth ;
153147 }
154148 // ensure whole width is visible on screen
155- if ( pos_screen_left + width + marginX > window . innerWidth ) {
156- pos_left = window . innerWidth - width - marginX ;
149+ if ( previewViewPosLeft + width + marginX > window . innerWidth ) {
150+ previewViewPosLeft = window . innerWidth - width - marginX ;
157151 }
158152 // ensure height is not bigger than window height
159153 if ( height > window . innerHeight ) {
160154 height = window . innerHeight ;
161155 }
162156
163157 // is the whole height (+ margin) not visible below the anchor
164- if ( pos_screen_top + height + 20 > window . innerHeight ) {
165- if ( window . innerHeight - position_anchor . top > position_anchor . top ) {
158+ if ( previewViewPosTop + height + 20 > window . innerHeight ) {
159+ if ( window . innerHeight - anchorViewPos . top > anchorViewPos . top ) {
166160 // more space below the anchor, so just adjust height
167- height = window . innerHeight - position_anchor . top - 20 ;
161+ height = window . innerHeight - anchorViewPos . top - 20 ;
168162 } else {
169163 // more space above the anchor, so move it there
170- pos_screen_top = position_anchor . top - height - 10 ;
171- if ( pos_screen_top < 10 ) {
164+ previewViewPosTop = anchorViewPos . top - height - 10 ;
165+ if ( previewViewPosTop < 10 ) {
172166 // shrink height to fit in screen
173- pos_screen_top = 10 ;
174- height = position_anchor . top - 20 ;
167+ previewViewPosTop = 10 ;
168+ height = anchorViewPos . top - 20 ;
175169 }
176170 }
177171 }
178172 // set preview position and size via css
179173 preview . style . width = width + "px" ;
180174 preview . style . height = height + "px" ;
181- preview . style . top = pos_screen_top + "px" ;
182- preview . style . left = pos_left + "px" ;
175+ preview . style . top = previewViewPosTop + "px" ;
176+ preview . style . left = previewViewPosLeft + "px" ;
183177 preview . style . position = "fixed" ;
184178}
185179
0 commit comments