Parcourir la source

:recycle: using scrollBehavior to intercept hashes from router

tags/1.0.0
J il y a 4 ans
Parent
révision
5f941f7931
2 fichiers modifiés avec 14 ajouts et 13 suppressions
  1. 2
    0
      vue-theme/src/components/sidebars/sidebar.vue
  2. 12
    13
      vue-theme/src/router/index.js

+ 2
- 0
vue-theme/src/components/sidebars/sidebar.vue Voir le fichier

8
                 li(v-for="option in sortOptions")
8
                 li(v-for="option in sortOptions")
9
                     router-link(:to="`/${type}/sorted/${sortTypes[option]}`")
9
                     router-link(:to="`/${type}/sorted/${sortTypes[option]}`")
10
                         p {{ option }}
10
                         p {{ option }}
11
+                li
11
                     router-link(:to="`/${type}`")
12
                     router-link(:to="`/${type}`")
12
                         p by all
13
                         p by all
14
+                li
13
                     router-link(v-if="shouldShowDateSort.includes(type) && layout !== 'single'" :to="`/${type}/sorted/by-current-and-upcoming`")
15
                     router-link(v-if="shouldShowDateSort.includes(type) && layout !== 'single'" :to="`/${type}/sorted/by-current-and-upcoming`")
14
                         p by current and upcoming
16
                         p by current and upcoming
15
         
17
         

+ 12
- 13
vue-theme/src/router/index.js Voir le fichier

6
 const router = createRouter({
6
 const router = createRouter({
7
     history: createWebHistory(),
7
     history: createWebHistory(),
8
     routes,
8
     routes,
9
+    scrollBehavior(to, from, savedPosition) {
10
+        let pos = { x: 0, y: 0 }
11
+        if (savedPosition) {
12
+            pos = savedPosition
13
+        }
14
+        if (to.hash) {
15
+            pos = { el: to.hash }
16
+        }
17
+        return pos
18
+    }
9
 })
19
 })
10
 
20
 
11
 router.beforeEach((to, from, next) => {
21
 router.beforeEach((to, from, next) => {
12
     document.title = 'Craft in America'
22
     document.title = 'Craft in America'
13
 
23
 
14
-    // Scroll to top
15
-    window.scrollTo(0, 0)
16
-
17
     // Append list type
24
     // Append list type
18
     if (to.path.split('/').length > 1 && to.path.split('/')[1]) {
25
     if (to.path.split('/').length > 1 && to.path.split('/')[1]) {
19
         const path = convertTitleCase(to.path.split('/')[1])
26
         const path = convertTitleCase(to.path.split('/')[1])
24
         document.title = `${document.title} - ${to.path.split('/')[2]}`
31
         document.title = `${document.title} - ${to.path.split('/')[2]}`
25
     }
32
     }
26
 
33
 
27
-    // Jump link intercept
28
-    if (to.hash) {
29
-        console.log('jump to:', to.hash)
30
-        const el = document.querySelector(to.hash)
31
-        const barHeight = 50
32
-        window.scrollTo(0, el.offsetTop - barHeight)
33
-    }
34
     // Regular links
34
     // Regular links
35
-    else {
36
-        next()
37
-    }
35
+    next()
36
+
38
 })
37
 })
39
 
38
 
40
 export default router
39
 export default router

Chargement…
Annuler
Enregistrer