Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/util/scroll.js
Original file line number Diff line number Diff line change
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)
}
}
}