Skip to content

Commit

Permalink
fix: drop pageXOffset and pageYOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Feb 14, 2023
1 parent 8509530 commit fc7bd97
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,7 @@ export let compute = (target: Element, options: Options): ScrollAction[] => {
// https://bokand.github.io/viewport/index.html
let viewportWidth = window.visualViewport?.width ?? innerWidth
let viewportHeight = window.visualViewport?.height ?? innerHeight

// Newer browsers supports scroll[X|Y], page[X|Y]Offset is
let viewportX = window.scrollX ?? pageXOffset
let viewportY = window.scrollY ?? pageYOffset
let { scrollX, scrollY } = window

let {
height: targetHeight,
Expand Down Expand Up @@ -436,13 +433,13 @@ export let compute = (target: Element, options: Options): ScrollAction[] => {
blockScroll = targetBlock - viewportHeight
} else if (block === 'nearest') {
blockScroll = alignNearest(
viewportY,
viewportY + viewportHeight,
scrollY,
scrollY + viewportHeight,
viewportHeight,
borderTop,
borderBottom,
viewportY + targetBlock,
viewportY + targetBlock + targetHeight,
scrollY + targetBlock,
scrollY + targetBlock + targetHeight,
targetHeight
)
} else {
Expand All @@ -459,24 +456,23 @@ export let compute = (target: Element, options: Options): ScrollAction[] => {
} else {
// inline === 'nearest' is the default
inlineScroll = alignNearest(
viewportX,
viewportX + viewportWidth,
scrollX,
scrollX + viewportWidth,
viewportWidth,
borderLeft,
borderRight,
viewportX + targetInline,
viewportX + targetInline + targetWidth,
scrollX + targetInline,
scrollX + targetInline + targetWidth,
targetWidth
)
}

// Apply scroll position offsets and ensure they are within bounds
// @TODO add more test cases to cover this 100%
blockScroll = Math.max(0, blockScroll + viewportY)
inlineScroll = Math.max(0, inlineScroll + viewportX)
blockScroll = Math.max(0, blockScroll + scrollY)
inlineScroll = Math.max(0, inlineScroll + scrollX)
} else {
// Handle each scrolling frame that might exist between the target and the viewport

if (block === 'start') {
blockScroll = targetBlock - top - borderTop
} else if (block === 'end') {
Expand Down

0 comments on commit fc7bd97

Please sign in to comment.