Skip to content

Commit 751de05

Browse files
committed
add scroll behavior example
1 parent 2aa5507 commit 751de05

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

examples/scroll-behavior/app.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Vue from 'vue/dist/vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
const Home = { template: '<div>home</div>' }
7+
const Foo = { template: '<div>foo</div>' }
8+
const Bar = { template: '<div>bar</div>' }
9+
10+
// scrollBehavior:
11+
// - only available in html5 history mode
12+
// - defaults to false
13+
// - if set to true, remembers and restores scroll position on popstate
14+
// - or use a function to conditionally return a position
15+
const scrollBehavior = (to, from, isPopState) => {
16+
if (!isPopState || to.matched.some(m => m.meta.scrollToTop)) {
17+
// explicitly control scroll behavior
18+
// scroll to top on new navigations or routes that requires scrolling to top
19+
return { x: 0, y: 0 }
20+
}
21+
// for popstate navigations, use saved position
22+
return true
23+
}
24+
25+
const router = new VueRouter({
26+
mode: 'history',
27+
base: __dirname,
28+
scrollBehavior,
29+
routes: [
30+
{ path: '/', component: Home, meta: { scrollToTop: true } },
31+
{ path: '/foo', component: Foo },
32+
{ path: '/bar', component: Bar }
33+
]
34+
})
35+
36+
const app = new Vue({
37+
router,
38+
template: `
39+
<div id="app">
40+
<h1>Basic</h1>
41+
<ul>
42+
<li><router-link to="/">/</router-link></li>
43+
<li><router-link to="/foo">/foo</router-link></li>
44+
<li><router-link to="/bar">/bar</router-link></li>
45+
</ul>
46+
<router-view class="view"></router-view>
47+
</div>
48+
`
49+
}).$mount('#app')

examples/scroll-behavior/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<style>
3+
.view {
4+
border: 1px solid red;
5+
height: 1000px;
6+
}
7+
</style>
8+
<a href="/">Examples index</a>
9+
<div id="app"></div>
10+
<script src="/__build__/shared.js"></script>
11+
<script src="/__build__/scroll-behavior.js"></script>

src/history/html5.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class HTML5History extends History {
2424
const current = this.current
2525
this.transitionTo(this.getLocation(), next => {
2626
if (expectScroll) {
27-
this.handleScroll(current, next)
27+
this.handleScroll(next, current, true)
2828
}
2929
})
3030
})
@@ -39,24 +39,26 @@ export class HTML5History extends History {
3939
}
4040

4141
push (location: RawLocation) {
42+
const current = this.current
4243
super.push(location, route => {
43-
const url = cleanPath(this.base + route.fullPath)
44-
pushState(url)
44+
pushState(cleanPath(this.base + route.fullPath))
45+
this.handleScroll(route, current, false)
4546
})
4647
}
4748

4849
replace (location: RawLocation) {
50+
const current = this.current
4951
super.replace(location, route => {
50-
const url = cleanPath(this.base + route.fullPath)
51-
replaceState(url)
52+
replaceState(cleanPath(this.base + route.fullPath))
53+
this.handleScroll(route, current, false)
5254
})
5355
}
5456

5557
getLocation (): string {
5658
return getLocation(this.base)
5759
}
5860

59-
handleScroll (from: Route, to: Route) {
61+
handleScroll (to: Route, from: Route, isPop: boolean) {
6062
const router = this.router
6163
if (!router.app) {
6264
return
@@ -67,7 +69,7 @@ export class HTML5History extends History {
6769
if (typeof behavior === 'boolean') {
6870
shouldScroll = behavior
6971
} else if (typeof behavior === 'function') {
70-
shouldScroll = behavior(from, to)
72+
shouldScroll = behavior(to, from, isPop)
7173
}
7274

7375
if (!shouldScroll) {

0 commit comments

Comments
 (0)