Skip to content
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

feat(scrollBehavior): Add container option #3581

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/util/scroll.js
Expand Up @@ -136,6 +136,12 @@ function isNumber (v: any): boolean {

const hashStartsWithNumberRE = /^#\d/

function getContainerElement (containerSelector: any): any {
return hashStartsWithNumberRE.test(containerSelector) // $flow-disable-line
? document.getElementById(containerSelector.slice(1)) // $flow-disable-line
: document.querySelector(containerSelector)
}

function scrollToPosition (shouldScroll, position) {
const isObject = typeof shouldScroll === 'object'
if (isObject && typeof shouldScroll.selector === 'string') {
Expand All @@ -160,16 +166,21 @@ function scrollToPosition (shouldScroll, position) {
}

if (position) {
let containerElement = window
if (isObject && typeof shouldScroll.container === 'string') {
containerElement = getContainerElement(shouldScroll.container)
}

// $flow-disable-line
if ('scrollBehavior' in document.documentElement.style) {
window.scrollTo({
containerElement.scrollTo({
left: position.x,
top: position.y,
// $flow-disable-line
behavior: shouldScroll.behavior
})
} else {
window.scrollTo(position.x, position.y)
containerElement.scrollTo(position.x, position.y)
}
}
}