Skip to content

Commit 5b0cdcf

Browse files
committed
feat($core): using 'smooth-scroll'
1 parent a3938b2 commit 5b0cdcf

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

packages/@vuepress/core/lib/app/app.js

+1-15
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,7 @@ export function createApp (isServer) {
6262
base: siteData.base,
6363
mode: 'history',
6464
fallback: false,
65-
routes,
66-
scrollBehavior: (to, from, saved) => {
67-
if (saved) {
68-
return saved
69-
} else if (to.hash) {
70-
if (Vue.$vuepress.$get('disableScrollBehavior')) {
71-
return false
72-
}
73-
return {
74-
selector: to.hash
75-
}
76-
} else {
77-
return { x: 0, y: 0 }
78-
}
79-
}
65+
routes
8066
})
8167

8268
// redirect /foo to /foo/

packages/@vuepress/core/lib/app/root-mixins/updateLoadingState.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1+
import SmoothScroll from 'smooth-scroll/dist/smooth-scroll.js'
2+
13
export default {
24
created () {
35
this.$vuepress.$on('AsyncMarkdownContentMounted', () => {
4-
this.$vuepress.$set('contentMounted', true);
6+
this.$vuepress.$set('contentMounted', true)
57

6-
[].slice.call(document.querySelectorAll('a[href^="#"]')).forEach(anchor => {
7-
anchor.addEventListener('click', function (e) {
8-
e.preventDefault()
9-
history.pushState(history.state, document.title, e.target.href)
10-
window.scroll({
11-
top: e.target.offsetTop - 75,
12-
left: 0,
13-
behavior: 'smooth'
14-
})
15-
})
8+
this.$smoothScroll = new SmoothScroll('a[href*="#"]', {
9+
speed: 1000,
10+
easing: 'easeInOutCubic'
1611
})
1712

1813
if (this.$route.hash) {
@@ -37,6 +32,11 @@ export default {
3732
watch: {
3833
'$route.path' () {
3934
this.$vuepress.$set('contentMounted', false)
35+
this.$smoothScroll.destroy()
4036
}
37+
},
38+
39+
beforeDestroy () {
40+
this.$smoothScroll.destroy()
4141
}
4242
}

packages/@vuepress/core/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
"webpack-chain": "^4.6.0",
6969
"webpack-merge": "^4.1.2",
7070
"webpack-serve": "^1.0.2",
71-
"webpackbar": "^2.6.1"
71+
"webpackbar": "^2.6.1",
72+
"smooth-scroll": "^15.0.0"
7273
},
7374
"engines": {
7475
"node": ">=8"

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -8119,6 +8119,10 @@ [email protected]:
81198119
dependencies:
81208120
is-fullwidth-code-point "^2.0.0"
81218121

8122+
smooth-scroll@^15.0.0:
8123+
version "15.0.0"
8124+
resolved "https://registry.yarnpkg.com/smooth-scroll/-/smooth-scroll-15.0.0.tgz#be4f9cb2cc4952d80db6736e5656ed5849305272"
8125+
81228126
snapdragon-node@^2.0.1:
81238127
version "2.1.1"
81248128
resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b"

0 commit comments

Comments
 (0)