Skip to content

Commit

Permalink
feat($core): using 'smooth-scroll'
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Nov 18, 2018
1 parent a3938b2 commit 5b0cdcf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
16 changes: 1 addition & 15 deletions packages/@vuepress/core/lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,7 @@ export function createApp (isServer) {
base: siteData.base,
mode: 'history',
fallback: false,
routes,
scrollBehavior: (to, from, saved) => {

This comment has been minimized.

Copy link
@janvennemann

janvennemann Dec 3, 2018

Contributor

@ulivz Was it intended to remove scrollBehavior completely? I now miss the scroll-to-top behavior when navigating between routes in the latest alpha.

This comment has been minimized.

Copy link
@ulivz

ulivz Dec 3, 2018

Author Member

Removing it is due to scrollBehavior of vue-router would introduce weird behaviors when combined with active-header-links.

I also realized that there are still some small issues with smoothing scroll, and I will take time to solve them ASAP.

At the same time, I also have the idea to remove the contentLoading which introduced some unpleasant codes.

Of course, welcome to contribute if you're sure you can make it better.

if (saved) {
return saved
} else if (to.hash) {
if (Vue.$vuepress.$get('disableScrollBehavior')) {
return false
}
return {
selector: to.hash
}
} else {
return { x: 0, y: 0 }
}
}
routes
})

// redirect /foo to /foo/
Expand Down
22 changes: 11 additions & 11 deletions packages/@vuepress/core/lib/app/root-mixins/updateLoadingState.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import SmoothScroll from 'smooth-scroll/dist/smooth-scroll.js'

export default {
created () {
this.$vuepress.$on('AsyncMarkdownContentMounted', () => {
this.$vuepress.$set('contentMounted', true);
this.$vuepress.$set('contentMounted', true)

[].slice.call(document.querySelectorAll('a[href^="#"]')).forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault()
history.pushState(history.state, document.title, e.target.href)
window.scroll({
top: e.target.offsetTop - 75,
left: 0,
behavior: 'smooth'
})
})
this.$smoothScroll = new SmoothScroll('a[href*="#"]', {
speed: 1000,

This comment has been minimized.

Copy link
@spiltcoffee

spiltcoffee Nov 22, 2018

Contributor

Unable to find a pull request for this, so maybe I'll ask here instead.

Is this meant to be 1000ms? By default, the speed option for smooth-scroll is how many milliseconds it should take to move 1000px, rather than to scroll from start to end. This results in a really long scrolling speed when scrolling a large distance, i.e. 3000px requires 3 seconds of scrolling rather than 1.

Considering how clunky this is, I feel like it wasn't intended.

If I may make a suggestion, add the option speedAsDuration: true to the initialisation of smooth-scroll? According to the docs, this should make the speed mean how long it should take to scroll from start to end (so 3000px is scrolled in 1 second rather than 3).

This comment has been minimized.

Copy link
@spiltcoffee

spiltcoffee Nov 22, 2018

Contributor

btw, happy to make a pull request for this if this sounds reasonable.

EDIT: fixed by 58981fb

easing: 'easeInOutCubic'
})

if (this.$route.hash) {
Expand All @@ -37,6 +32,11 @@ export default {
watch: {
'$route.path' () {
this.$vuepress.$set('contentMounted', false)
this.$smoothScroll.destroy()
}
},

beforeDestroy () {
this.$smoothScroll.destroy()
}
}
3 changes: 2 additions & 1 deletion packages/@vuepress/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"webpack-chain": "^4.6.0",
"webpack-merge": "^4.1.2",
"webpack-serve": "^1.0.2",
"webpackbar": "^2.6.1"
"webpackbar": "^2.6.1",
"smooth-scroll": "^15.0.0"
},
"engines": {
"node": ">=8"
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8119,6 +8119,10 @@ [email protected]:
dependencies:
is-fullwidth-code-point "^2.0.0"

smooth-scroll@^15.0.0:
version "15.0.0"
resolved "https://registry.yarnpkg.com/smooth-scroll/-/smooth-scroll-15.0.0.tgz#be4f9cb2cc4952d80db6736e5656ed5849305272"

snapdragon-node@^2.0.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b"
Expand Down

0 comments on commit 5b0cdcf

Please sign in to comment.