Skip to content
This repository has been archived by the owner on Dec 24, 2023. It is now read-only.

Commit

Permalink
fix: this fixes issue 13 and updates dev dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
wswebcreation committed Aug 20, 2019
1 parent 7544c86 commit f96ebc7
Show file tree
Hide file tree
Showing 4 changed files with 555 additions and 585 deletions.
27 changes: 0 additions & 27 deletions lib/clientSideScripts/scrollToPosition.spec.ts

This file was deleted.

55 changes: 3 additions & 52 deletions lib/clientSideScripts/scrollToPosition.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,8 @@
/**
* Scroll to a x = 0 and y = variable position in the screen
* Sometimes a scroll can't be done on the window, so the scroll needs to be done on the largest
* element on the screen
* Scroll to y = variable position in the screen
*/
/* istanbul ignore next */
export default function scrollToPosition(yPosition: number): void {
const initialScroll = document.documentElement.scrollTop;

// If for some reason the scroll didn't work, find the first largest element and use that to scroll on
let largestNode: HTMLElement;

// Scroll with the default way of scrolling
window.scrollTo(0, yPosition);

/* istanbul ignore else */
if (initialScroll === document.documentElement.scrollTop && yPosition > 0) {
largestFirstNode(document.documentElement.childNodes).scrollTo(0, yPosition);
}

/**
* Find the first largest node in the page so it can be used for scrolling
*/
function largestFirstNode(nodesList: any) {
let node: HTMLElement;

// Loop over the nodes
for (let i = nodesList.length - 1; i >= 0; i--) {
node = nodesList[i];

// Stop if the largest node has been found
/* istanbul ignore next */
if (largestNode) {
break;
}

// Check if the element has a scroll / client height
/* istanbul ignore next */
if (node.scrollHeight && node.clientHeight) {
const viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
const elHeight = Math.max(node.scrollHeight, node.clientHeight);

// If the element is bigger than the viewport, then we got our scroll container
if (elHeight > viewPortHeight) {
largestNode = node;
break;
}
}

if (node.childNodes.length) {
largestFirstNode(node.childNodes);
}
}

// If the largestNode is the body then return the window
return (document.body === largestNode || !largestNode) ? window : largestNode;
}
(document.scrollingElement || document.documentElement).scrollTop = yPosition;
}

0 comments on commit f96ebc7

Please sign in to comment.