Skip to content

Commit 6039549

Browse files
committed
use scrollTo with options to avoid smooth scroll on load instead of chaning scrollBehavior style
1 parent 301a529 commit 6039549

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/js/03-fragment-jumper.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
var article = document.querySelector('article.doc')
55
var toolbar = document.querySelector('.toolbar')
6+
var supportsScrollToOptions = 'scrollTo' in document.documentElement
67

78
function decodeFragment (hash) {
89
return hash && (~hash.indexOf('%') ? decodeURIComponent(hash) : hash).slice(1)
@@ -18,18 +19,16 @@
1819
window.location.hash = '#' + this.id
1920
e.preventDefault()
2021
}
21-
window.scrollTo(0, computePosition(this, 0) - toolbar.getBoundingClientRect().bottom)
22+
var y = computePosition(this, 0) - toolbar.getBoundingClientRect().bottom
23+
var instant = e === false && supportsScrollToOptions
24+
instant ? window.scrollTo({ left: 0, top: y, behavior: 'instant' }) : window.scrollTo(0, y)
2225
}
2326

2427
window.addEventListener('load', function jumpOnLoad (e) {
2528
var fragment, target
2629
if ((fragment = decodeFragment(window.location.hash)) && (target = document.getElementById(fragment))) {
27-
document.documentElement.style.scrollBehavior = 'auto'
28-
jumpToAnchor.call(target)
29-
setTimeout(jumpToAnchor.bind(target), 0)
30-
setTimeout(function () {
31-
document.documentElement.style.scrollBehavior = ''
32-
})
30+
jumpToAnchor.call(target, false)
31+
setTimeout(jumpToAnchor.bind(target, false), 250)
3332
}
3433
window.removeEventListener('load', jumpOnLoad)
3534
})

0 commit comments

Comments
 (0)