Skip to content

Commit ae3da33

Browse files
authored
Merge pull request #9 from decates/add-delay
Add delay
2 parents de5ccbf + 7803679 commit ae3da33

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ When using client-side routing, we may want to scroll to top when navigating to
6767

6868
**Of course, If you have some special scenes, we also provide some options, and you can manually use them to save or restore the scroll position**
6969

70+
**If you use transitions on all of your route changes, use the _delay_ option to delay the scroll until the appropriate point (e.g. the middle of the changeover).**
7071
## Features
7172

7273
* **Simplicity** - only need to call `Vue.vueScrollBehavior(router)`
@@ -87,7 +88,8 @@ import vueScrollBehavior from 'vue-scroll-behavior'
8788
Vue.use(vueScrollBehavior, {
8889
router: router, // The router instance
8990
maxLength: 100, // Saved history List max length
90-
ignore: [/\/boo/, /\/zoo/] // ignore some routes, they will directly scroll to the top
91+
ignore: [/\/boo/, /\/zoo/], // ignore some routes, they will directly scroll to the top
92+
delay: 0 // Delay by a number of milliseconds
9193
})
9294
```
9395

@@ -112,6 +114,7 @@ Prop | Data Type | Default | Description
112114
`router` | Object | | The router instance: `const router = new VueRouter({})`
113115
`ignore` | Array | `[ ]` | **RegExp** list to ignore some routes, they will directly scroll to the top
114116
`maxLength` | Number | `50` | Saved history List max length
117+
`delay` | Number | `0` | Delay scroll by a number of milliseconds
115118

116119

117120
## ChangeLog

dist/vue-scroll-behavior.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/helpers.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export function setOption (options) {
1212
if (typeof options.ignore !== 'undefined' && Array.isArray(options.ignore)) {
1313
vueScrollBehavior._ignore = options.ignore
1414
}
15+
16+
if (typeof options.delay === 'number') {
17+
vueScrollBehavior._delay = options.delay
18+
}
1519
}
1620

1721
/**
@@ -28,9 +32,17 @@ export function getScrollPosition () {
2832
* Setting Scroll Position
2933
*/
3034
export function setScrollPosition (Vue, position = {x: 0, y: 0}) {
31-
Vue.nextTick(() => {
32-
window.scrollTo(position.x, position.y)
33-
})
35+
if (vueScrollBehavior._delay > 0) {
36+
setTimeout(() => {
37+
Vue.nextTick(() => {
38+
window.scrollTo(position.x, position.y)
39+
})
40+
}, vueScrollBehavior._delay);
41+
} else {
42+
Vue.nextTick(() => {
43+
window.scrollTo(position.x, position.y)
44+
})
45+
}
3446
}
3547

3648
/**

src/vue-scroll-behavior.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { setOption, isIgnoreRoute, getScrollPosition, setScrollPosition,
99

1010
const vueScrollBehavior = {
1111
_maxLength: 50,
12-
_ignore: []
12+
_ignore: [],
13+
_delay: 0
1314
}
1415

1516
/**

0 commit comments

Comments
 (0)