@@ -226,12 +226,15 @@ export const compute = (
226226 }
227227
228228 // Workaround Chrome's behavior on clientHeight/clientWidth after introducing visualViewport
229+ // https://www.quirksmode.org/blog/archives/2016/02/chrome_change_b.html
229230 const viewportWidth = window . visualViewport
230231 ? window . visualViewport . width
231232 : viewport . clientWidth
232233 const viewportHeight = window . visualViewport
233234 ? window . visualViewport . height
234235 : viewport . clientHeight
236+ const viewportX = window . scrollX || window . pageXOffset
237+ const viewportY = window . scrollY || window . pageYOffset
235238
236239 // If the element is already visible we can end it here
237240 if ( scrollMode === 'if-needed' ) {
@@ -284,7 +287,7 @@ export const compute = (
284287 targetBlock = targetRect . top
285288 }
286289 if ( viewport === frame ) {
287- blockScroll = frame . scrollTop + targetBlock
290+ blockScroll = viewportY + targetBlock
288291 } else {
289292 // prevent scrollTop values that overflow the scrollHeight
290293 const offset = Math . min (
@@ -301,7 +304,7 @@ export const compute = (
301304 targetBlock = targetRect . top + targetRect . height / 2
302305 }
303306 if ( viewport === frame ) {
304- blockScroll = frame . scrollTop + targetBlock - frame . clientHeight / 2
307+ blockScroll = viewportY + targetBlock - frame . clientHeight / 2
305308 } else {
306309 // prevent negative scrollTop values
307310 const offset =
@@ -323,7 +326,7 @@ export const compute = (
323326 targetBlock = targetRect . bottom
324327 }
325328 if ( viewport === frame ) {
326- blockScroll = frame . scrollTop + targetBlock - frame . clientHeight
329+ blockScroll = viewportY + targetBlock - frame . clientHeight
327330 } else {
328331 // prevent negative scrollTop values
329332 const offset =
@@ -343,15 +346,15 @@ export const compute = (
343346
344347 if ( viewport === frame ) {
345348 const offset = alignNearest (
346- frame . scrollTop ,
347- frame . scrollTop + viewportHeight ,
349+ viewportY ,
350+ viewportY + viewportHeight ,
348351 viewportHeight ,
349- frame . scrollTop + targetBlock ,
350- frame . scrollTop + targetBlock + targetRect . height ,
352+ viewportY + targetBlock ,
353+ viewportY + targetBlock + targetRect . height ,
351354 targetRect . height
352355 )
353356
354- blockScroll = frame . scrollTop + offset
357+ blockScroll = viewportY + offset
355358 } else {
356359 const offset = alignNearest (
357360 frameRect . top ,
@@ -373,7 +376,7 @@ export const compute = (
373376 targetInline = targetRect . left
374377 }
375378 if ( viewport === frame ) {
376- inlineScroll = frame . scrollLeft + targetInline
379+ inlineScroll = viewportX + targetInline
377380 } else {
378381 // prevent scrollLeft values that overflow the scrollLeft
379382 const offset = Math . min (
@@ -391,7 +394,7 @@ export const compute = (
391394 targetInline = targetRect . left + targetRect . width / 2
392395 }
393396 if ( viewport === frame ) {
394- inlineScroll = frame . scrollLeft + targetInline - frame . clientWidth / 2
397+ inlineScroll = viewportX + targetInline - frame . clientWidth / 2
395398 } else {
396399 // prevent negative scrollLeft values
397400 const offset =
@@ -413,7 +416,7 @@ export const compute = (
413416 targetInline = targetRect . right
414417 }
415418 if ( viewport === frame ) {
416- inlineScroll = frame . scrollLeft + targetInline - frame . clientWidth
419+ inlineScroll = viewportX + targetInline - frame . clientWidth
417420 } else {
418421 // prevent negative scrollLeft values
419422 const offset =
@@ -433,15 +436,15 @@ export const compute = (
433436
434437 if ( viewport === frame ) {
435438 const offset = alignNearest (
436- frame . scrollLeft ,
437- frame . scrollLeft + viewportWidth ,
439+ viewportX ,
440+ viewportX + viewportWidth ,
438441 viewportWidth ,
439- frame . scrollLeft + targetInline ,
440- frame . scrollLeft + targetInline + targetRect . width ,
442+ viewportX + targetInline ,
443+ viewportX + targetInline + targetRect . width ,
441444 targetRect . width
442445 )
443446
444- inlineScroll = frame . scrollLeft + offset
447+ inlineScroll = viewportX + offset
445448 } else {
446449 const offset = alignNearest (
447450 frameRect . left ,
0 commit comments