Bladeren bron

:sparkles: jumplink router guard

tags/0.9.0
j 4 jaren geleden
bovenliggende
commit
38571fb7d7
1 gewijzigde bestanden met toevoegingen van 13 en 2 verwijderingen
  1. 13
    2
      vue-theme/src/router/index.js

+ 13
- 2
vue-theme/src/router/index.js Bestand weergeven

@@ -20,10 +20,21 @@ router.beforeEach((to, from, next) => {
20 20
         document.title = `${document.title} - ${path}`
21 21
     }
22 22
     // Append slugs and sort titles
23
-    if (to.path.split('/')[2])
23
+    if (to.path.split('/')[2]) {
24 24
         document.title = `${document.title} - ${to.path.split('/')[2]}`
25
+    }
25 26
 
26
-    next()
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
35
+    else {
36
+        next()
37
+    }
27 38
 })
28 39
 
29 40
 export default router

Laden…
Annuleren
Opslaan