-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance hashHistory to support scrollBehavior (#1662)
* enhance hashhistory to support scrollbehavior * fix ensure slash
- Loading branch information
Showing
4 changed files
with
187 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import Vue from 'vue' | ||
import VueRouter from 'vue-router' | ||
|
||
Vue.use(VueRouter) | ||
|
||
const Home = { template: '<div>home</div>' } | ||
const Foo = { template: '<div>foo</div>' } | ||
const Bar = { | ||
template: ` | ||
<div> | ||
bar | ||
<div style="height:500px"></div> | ||
<p id="anchor" style="height:500px">Anchor</p> | ||
<p id="anchor2">Anchor2</p> | ||
</div> | ||
` | ||
} | ||
|
||
// scrollBehavior: | ||
// - only available in html5 history mode | ||
// - defaults to no scroll behavior | ||
// - return false to prevent scroll | ||
const scrollBehavior = (to, from, savedPosition) => { | ||
if (savedPosition) { | ||
// savedPosition is only available for popstate navigations. | ||
return savedPosition | ||
} else { | ||
const position = {} | ||
// new navigation. | ||
// scroll to anchor by returning the selector | ||
if (to.hash) { | ||
position.selector = to.hash | ||
console.log(to) | ||
|
||
// specify offset of the element | ||
if (to.hash === '#anchor2') { | ||
position.offset = { y: 100 } | ||
} | ||
} | ||
// check if any matched route config has meta that requires scrolling to top | ||
if (to.matched.some(m => m.meta.scrollToTop)) { | ||
// cords will be used if no selector is provided, | ||
// or if the selector didn't match any element. | ||
position.x = 0 | ||
position.y = 0 | ||
} | ||
// if the returned position is falsy or an empty object, | ||
// will retain current scroll position. | ||
return position | ||
} | ||
} | ||
|
||
const router = new VueRouter({ | ||
mode: 'hash', | ||
scrollBehavior, | ||
routes: [ | ||
{ path: '/', component: Home, meta: { scrollToTop: true }}, | ||
{ path: '/foo', component: Foo }, | ||
{ path: '/bar', component: Bar, meta: { scrollToTop: true }} | ||
] | ||
}) | ||
|
||
new Vue({ | ||
router, | ||
template: ` | ||
<div id="app"> | ||
<h1>Scroll Behavior</h1> | ||
<ul> | ||
<li><router-link to="/">/</router-link></li> | ||
<li><router-link to="/foo">/foo</router-link></li> | ||
<li><router-link to="/bar">/bar</router-link></li> | ||
<li><router-link to="/bar#anchor">/bar#anchor</router-link></li> | ||
<li><router-link to="/bar#anchor2">/bar#anchor2</router-link></li> | ||
</ul> | ||
<router-view class="view"></router-view> | ||
</div> | ||
` | ||
}).$mount('#app') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<link rel="stylesheet" href="/global.css"> | ||
<style> | ||
.view { | ||
border: 1px solid red; | ||
height: 2000px; | ||
position: relative; | ||
} | ||
</style> | ||
<a href="/">← Examples index</a> | ||
<div id="app"></div> | ||
<script src="/__build__/shared.js"></script> | ||
<script src="/__build__/hash-scroll-behavior.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module.exports = { | ||
'scroll behavior': function (browser) { | ||
browser | ||
.url('http://localhost:8080/hash-scroll-behavior/') | ||
.waitForElementVisible('#app', 1000) | ||
.assert.count('li a', 5) | ||
.assert.containsText('.view', 'home') | ||
|
||
.execute(function () { | ||
window.scrollTo(0, 100) | ||
}) | ||
.click('li:nth-child(2) a') | ||
.assert.containsText('.view', 'foo') | ||
.execute(function () { | ||
window.scrollTo(0, 200) | ||
window.history.back() | ||
}) | ||
.assert.containsText('.view', 'home') | ||
.assert.evaluate(function () { | ||
return window.pageYOffset === 100 | ||
}, null, 'restore scroll position on back') | ||
|
||
// scroll on a popped entry | ||
.execute(function () { | ||
window.scrollTo(0, 50) | ||
window.history.forward() | ||
}) | ||
.assert.containsText('.view', 'foo') | ||
.assert.evaluate(function () { | ||
return window.pageYOffset === 200 | ||
}, null, 'restore scroll position on forward') | ||
|
||
.execute(function () { | ||
window.history.back() | ||
}) | ||
.assert.containsText('.view', 'home') | ||
.assert.evaluate(function () { | ||
return window.pageYOffset === 50 | ||
}, null, 'restore scroll position on back again') | ||
|
||
.click('li:nth-child(3) a') | ||
.assert.evaluate(function () { | ||
return window.pageYOffset === 0 | ||
}, null, 'scroll to top on new entry') | ||
|
||
.click('li:nth-child(4) a') | ||
.assert.evaluate(function () { | ||
return document.getElementById('anchor').getBoundingClientRect().top < 1 | ||
}, null, 'scroll to anchor') | ||
|
||
.click('li:nth-child(5) a') | ||
.assert.evaluate(function () { | ||
return document.getElementById('anchor2').getBoundingClientRect().top < 101 | ||
}, null, 'scroll to anchor with offset') | ||
.end() | ||
} | ||
} |