-
-
Notifications
You must be signed in to change notification settings - Fork 119
update to simplify scroll sync behavior #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
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 | ||
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new way is more similar to how Stripe does it: |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was not always ideal and felt buggy since it would just barely keep the items in view.