@@ -226,12 +226,15 @@ export const compute = (
226
226
}
227
227
228
228
// Workaround Chrome's behavior on clientHeight/clientWidth after introducing visualViewport
229
+ // https://www.quirksmode.org/blog/archives/2016/02/chrome_change_b.html
229
230
const viewportWidth = window . visualViewport
230
231
? window . visualViewport . width
231
232
: viewport . clientWidth
232
233
const viewportHeight = window . visualViewport
233
234
? window . visualViewport . height
234
235
: viewport . clientHeight
236
+ const viewportX = window . scrollX || window . pageXOffset
237
+ const viewportY = window . scrollY || window . pageYOffset
235
238
236
239
// If the element is already visible we can end it here
237
240
if ( scrollMode === 'if-needed' ) {
@@ -284,7 +287,7 @@ export const compute = (
284
287
targetBlock = targetRect . top
285
288
}
286
289
if ( viewport === frame ) {
287
- blockScroll = frame . scrollTop + targetBlock
290
+ blockScroll = viewportY + targetBlock
288
291
} else {
289
292
// prevent scrollTop values that overflow the scrollHeight
290
293
const offset = Math . min (
@@ -301,7 +304,7 @@ export const compute = (
301
304
targetBlock = targetRect . top + targetRect . height / 2
302
305
}
303
306
if ( viewport === frame ) {
304
- blockScroll = frame . scrollTop + targetBlock - frame . clientHeight / 2
307
+ blockScroll = viewportY + targetBlock - frame . clientHeight / 2
305
308
} else {
306
309
// prevent negative scrollTop values
307
310
const offset =
@@ -323,7 +326,7 @@ export const compute = (
323
326
targetBlock = targetRect . bottom
324
327
}
325
328
if ( viewport === frame ) {
326
- blockScroll = frame . scrollTop + targetBlock - frame . clientHeight
329
+ blockScroll = viewportY + targetBlock - frame . clientHeight
327
330
} else {
328
331
// prevent negative scrollTop values
329
332
const offset =
@@ -343,15 +346,15 @@ export const compute = (
343
346
344
347
if ( viewport === frame ) {
345
348
const offset = alignNearest (
346
- frame . scrollTop ,
347
- frame . scrollTop + viewportHeight ,
349
+ viewportY ,
350
+ viewportY + viewportHeight ,
348
351
viewportHeight ,
349
- frame . scrollTop + targetBlock ,
350
- frame . scrollTop + targetBlock + targetRect . height ,
352
+ viewportY + targetBlock ,
353
+ viewportY + targetBlock + targetRect . height ,
351
354
targetRect . height
352
355
)
353
356
354
- blockScroll = frame . scrollTop + offset
357
+ blockScroll = viewportY + offset
355
358
} else {
356
359
const offset = alignNearest (
357
360
frameRect . top ,
@@ -373,7 +376,7 @@ export const compute = (
373
376
targetInline = targetRect . left
374
377
}
375
378
if ( viewport === frame ) {
376
- inlineScroll = frame . scrollLeft + targetInline
379
+ inlineScroll = viewportX + targetInline
377
380
} else {
378
381
// prevent scrollLeft values that overflow the scrollLeft
379
382
const offset = Math . min (
@@ -391,7 +394,7 @@ export const compute = (
391
394
targetInline = targetRect . left + targetRect . width / 2
392
395
}
393
396
if ( viewport === frame ) {
394
- inlineScroll = frame . scrollLeft + targetInline - frame . clientWidth / 2
397
+ inlineScroll = viewportX + targetInline - frame . clientWidth / 2
395
398
} else {
396
399
// prevent negative scrollLeft values
397
400
const offset =
@@ -413,7 +416,7 @@ export const compute = (
413
416
targetInline = targetRect . right
414
417
}
415
418
if ( viewport === frame ) {
416
- inlineScroll = frame . scrollLeft + targetInline - frame . clientWidth
419
+ inlineScroll = viewportX + targetInline - frame . clientWidth
417
420
} else {
418
421
// prevent negative scrollLeft values
419
422
const offset =
@@ -433,15 +436,15 @@ export const compute = (
433
436
434
437
if ( viewport === frame ) {
435
438
const offset = alignNearest (
436
- frame . scrollLeft ,
437
- frame . scrollLeft + viewportWidth ,
439
+ viewportX ,
440
+ viewportX + viewportWidth ,
438
441
viewportWidth ,
439
- frame . scrollLeft + targetInline ,
440
- frame . scrollLeft + targetInline + targetRect . width ,
442
+ viewportX + targetInline ,
443
+ viewportX + targetInline + targetRect . width ,
441
444
targetRect . width
442
445
)
443
446
444
- inlineScroll = frame . scrollLeft + offset
447
+ inlineScroll = viewportX + offset
445
448
} else {
446
449
const offset = alignNearest (
447
450
frameRect . left ,
0 commit comments