Skip to content

Commit 06abe79

Browse files
committed
feat: add anchor
1 parent 681be0c commit 06abe79

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<template>
2+
<Toc class="docs-anchor" />
3+
</template>
4+
5+
<style lang="scss">
6+
.vuepress-toc.docs-anchor {
7+
position: fixed;
8+
top: 115px;
9+
right: 20px;
10+
ul.vuepress-toc-list {
11+
list-style: none;
12+
font-size: 12px;
13+
max-width: 125px;
14+
min-width: 92px;
15+
max-height: 80vh;
16+
border-left: 1px solid var(--c-border-dark);
17+
margin: 0;
18+
li.vuepress-toc-item {
19+
a {
20+
color: var(--text-color);
21+
opacity: 0.85;
22+
&:hover {
23+
opacity: 1;
24+
color: var(--c-text-accent);
25+
transition: all 0.3s;
26+
}
27+
}
28+
a.router-link-active.active {
29+
color: var(--c-text-accent);
30+
}
31+
&:not(:last-child) {
32+
margin-bottom: 4px;
33+
}
34+
&:empty,
35+
& > .vuepress-toc-list {
36+
display: none;
37+
}
38+
}
39+
}
40+
}
41+
ul.vuepress-toc-list > li.vuepress-toc-item:nth-last-child(1):first-child {
42+
// 只有一个item时隐藏
43+
display: none;
44+
}
45+
@media only screen and (max-width: 1280px) {
46+
.docs-anchor {
47+
display: none;
48+
}
49+
}
50+
</style>

docs/.vuepress/layout/layout.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<template>
22
<Layout>
3+
<template #page-top>
4+
<Anchor v-show="anchorRight" />
5+
</template>
36
<template #page-bottom>
47
<footer class="docs-footer page-meta">
58
<div :class="$style.footerContainer" role="presentation">
@@ -13,7 +16,12 @@
1316
</template>
1417

1518
<script setup lang="ts">
19+
import { ref } from 'vue'
20+
import { usePageFrontmatter } from '@vuepress/client'
1621
import Layout from '@vuepress/theme-default/lib/client/layouts/Layout.vue'
22+
23+
const frontmatter = usePageFrontmatter()
24+
const anchorRight = ref(frontmatter.value.anchorRight ?? true)
1725
</script>
1826

1927
<style module lang="scss">

0 commit comments

Comments
 (0)