antfu/vue-router-better-scroller
소개
Enhanced scroll behavior for Vue Router
웹 및 앱 개발측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- -1
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Originalvue-router-better-scroller
Enhanced scroll behavior for Vue Router v4.
Motivation
Vue Router currently only preserves the scrolling state on the window object. Sometimes, in your apps you might have a different scrollable element (e.g. body, #app) or even multiple ones. To gain a better user experience, we typically want to preserve the scroll state of them when going back and forth.
This plugin is introduced to experiment with a better way to handle such cases. With a lot of help from @posva 🙏.
Install
npm i vue-router-better-scroller
In your main entry:
import { createRouter, createWebHistory } from 'vue-router'
import { createRouterScroller } from 'vue-router-better-scroller'
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
const router = createRouter({
history: createWebHistory(),
routes
})
app.use(router)
app.use(createRouterScroller({
selectors: {
'window': true,
'body': true,
'.scrollable': true
},
}))
app.mount('#app')
Options
selectors
This plugin supports preserving the state of multiple scrollable elements. By passing the selectors object, you can specify which elements you want to preserve the scroll state.
When set to true, we will preserve and restore the scroll state of them automatically, when users navigate back and forth (but not RouteLink or router.push navigation).
You can also pass a custom handler:
createRouterScroller({
selectors: {
// use default handler for `window`
window: true,
// custom handler for scrolling on `body`
body({ to, from, type, savedPosition, element }) {
// navigation triggered by RouteLink or router.push
if (type === 'push') {
return false // disable scroll restoration
}
// navigation triggered by browser back/forward, or router.back()
else if (type === 'history') {
if (to.fullPath === '/') {
// return a custom position
return {
top: 10
}
}
// custom handling
element.scrollTo({
...savedPosition,
behavior: 'smooth',
})
}
},
},
})
Sponsors
License
MIT License © 2022 Anthony Fu
관련 프로젝트 살펴보기
불러오는 중…