Skip to content

Add offset feature to scrollBehavior #1501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion examples/scroll-behavior/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const Bar = {
<div>
bar
<div style="height:500px"></div>
<p id="anchor">Anchor</p>
<p id="anchor" style="height:500px">Anchor</p>
<p id="anchor2">Anchor2</p>
</div>
`
}
Expand All @@ -29,6 +30,11 @@ const scrollBehavior = (to, from, savedPosition) => {
// scroll to anchor by returning the selector
if (to.hash) {
position.selector = to.hash

// 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)) {
Expand Down Expand Up @@ -64,6 +70,7 @@ new Vue({
<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>
Expand Down
17 changes: 13 additions & 4 deletions src/util/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function handleScroll (
if (isObject && typeof shouldScroll.selector === 'string') {
const el = document.querySelector(shouldScroll.selector)
if (el) {
position = getElementPosition(el)
let offset = shouldScroll.offset && typeof shouldScroll.offset === 'object' ? shouldScroll.offset : {}
offset = normalizeOffset(offset)
position = getElementPosition(el, offset)
} else if (isValidPosition(shouldScroll)) {
position = normalizePosition(shouldScroll)
}
Expand Down Expand Up @@ -76,13 +78,13 @@ function getScrollPosition (): ?Object {
}
}

function getElementPosition (el: Element): Object {
function getElementPosition (el: Element, offset: Object): Object {
const docEl: any = document.documentElement
const docRect = docEl.getBoundingClientRect()
const elRect = el.getBoundingClientRect()
return {
x: elRect.left - docRect.left,
y: elRect.top - docRect.top
x: elRect.left - docRect.left - offset.x,
y: elRect.top - docRect.top - offset.y
}
}

Expand All @@ -97,6 +99,13 @@ function normalizePosition (obj: Object): Object {
}
}

function normalizeOffset (obj: Object): Object {
return {
x: isNumber(obj.x) ? obj.x : 0,
y: isNumber(obj.y) ? obj.y : 0
}
}

function isNumber (v: any): boolean {
return typeof v === 'number'
}
7 changes: 6 additions & 1 deletion test/e2e/specs/scroll-behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
browser
.url('http://localhost:8080/scroll-behavior/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 4)
.assert.count('li a', 5)
.assert.containsText('.view', 'home')

.execute(function () {
Expand Down Expand Up @@ -47,6 +47,11 @@ module.exports = {
.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()
}
}