-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update to simplify scroll sync behavior (#358)
* update to simplify scroll sync behavior to be simpler and easier to follow * built files
- Loading branch information
Showing
7 changed files
with
19 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,16 @@ | ||
const SCROLL_LEEWAY = 30 | ||
|
||
export default function updateTocScroll (options) { | ||
const toc = options.tocScrollingWrapper || options.tocElement || document.querySelector(options.tocSelector) | ||
if (toc && toc.scrollHeight > toc.clientHeight) { | ||
const activeItem = toc.querySelector(`.${options.activeListItemClass}`) | ||
if (activeItem) { | ||
// Determine container top and bottom | ||
const cTop = toc.scrollTop | ||
const cBottom = cTop + toc.clientHeight | ||
|
||
// Determine element top and bottom | ||
const eTop = activeItem.offsetTop | ||
const eBottom = eTop + activeItem.clientHeight | ||
|
||
// Check if out of view | ||
// Above scroll view | ||
if (eTop < cTop + options.tocScrollOffset) { | ||
toc.scrollTop -= (cTop - eTop) + options.tocScrollOffset | ||
// Below scroll view | ||
} else if (eBottom > cBottom - options.tocScrollOffset - SCROLL_LEEWAY) { | ||
toc.scrollTop += (eBottom - cBottom) + options.tocScrollOffset + (2 * SCROLL_LEEWAY) | ||
} | ||
const scrollAmount = eTop - options.tocScrollOffset | ||
toc.scrollTop = scrollAmount > 0 ? scrollAmount : 0 | ||
} | ||
} | ||
} |