aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/docs/app/router.options.ts
blob: 9380f053a321d07a2b42fe60de8c4b56a9e4ebb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { RouterConfig } from '@nuxt/schema'
// https://router.vuejs.org/api/interfaces/routeroptions.html
export default <RouterConfig> {
  scrollBehavior (to, _form, savedPosition) {
    if (history.state.stop) { return }

    if (history.state.smooth) {
      return {
        el: history.state.smooth,
        behavior: 'smooth'
      }
    }

    if (to.hash) {
      const el = document.querySelector(to.hash) as any

      if (!el) { return }

      const { marginTop } = getComputedStyle(el)

      const marginTopValue = parseInt(marginTop)

      const offset = (document.querySelector(to.hash) as any).offsetTop - marginTopValue

      return {
        top: offset,
        behavior: 'smooth'
      }
    }

    // Scroll to top of window
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
}