md2ui 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,77 @@
1
+ import { ref } from 'vue'
2
+
3
+ export function useScroll() {
4
+ const scrollProgress = ref(0)
5
+ const showBackToTop = ref(false)
6
+ const activeHeading = ref('')
7
+
8
+ // 监听滚动
9
+ function handleScroll(e) {
10
+ const element = e.target
11
+ const scrollTop = element.scrollTop
12
+ const scrollHeight = element.scrollHeight - element.clientHeight
13
+
14
+ if (scrollHeight > 0) {
15
+ scrollProgress.value = Math.round((scrollTop / scrollHeight) * 100)
16
+ showBackToTop.value = scrollTop > 300
17
+ }
18
+
19
+ updateActiveHeading()
20
+ }
21
+
22
+ // 更新当前激活的标题
23
+ function updateActiveHeading() {
24
+ const content = document.querySelector('.content')
25
+ if (!content) return
26
+
27
+ const headings = document.querySelectorAll('.markdown-content h1, .markdown-content h2, .markdown-content h3, .markdown-content h4, .markdown-content h5, .markdown-content h6')
28
+ const scrollTop = content.scrollTop
29
+
30
+ let currentId = ''
31
+ headings.forEach(heading => {
32
+ const rect = heading.getBoundingClientRect()
33
+ const contentRect = content.getBoundingClientRect()
34
+ const offsetTop = rect.top - contentRect.top + scrollTop
35
+
36
+ if (offsetTop <= scrollTop + 100) {
37
+ currentId = heading.id
38
+ }
39
+ })
40
+
41
+ activeHeading.value = currentId
42
+ }
43
+
44
+ // 滚动到指定标题
45
+ function scrollToHeading(id) {
46
+ const element = document.getElementById(id)
47
+ const content = document.querySelector('.content')
48
+
49
+ if (element && content) {
50
+ const contentRect = content.getBoundingClientRect()
51
+ const elementRect = element.getBoundingClientRect()
52
+ const offsetTop = elementRect.top - contentRect.top + content.scrollTop
53
+
54
+ content.scrollTo({
55
+ top: offsetTop - 20,
56
+ behavior: 'smooth'
57
+ })
58
+ }
59
+ }
60
+
61
+ // 返回顶部
62
+ function scrollToTop() {
63
+ const content = document.querySelector('.content')
64
+ if (content) {
65
+ content.scrollTo({ top: 0, behavior: 'smooth' })
66
+ }
67
+ }
68
+
69
+ return {
70
+ scrollProgress,
71
+ showBackToTop,
72
+ activeHeading,
73
+ handleScroll,
74
+ scrollToHeading,
75
+ scrollToTop
76
+ }
77
+ }
package/src/main.js ADDED
@@ -0,0 +1,5 @@
1
+ import { createApp } from 'vue'
2
+ import App from './App.vue'
3
+ import './style.css'
4
+
5
+ createApp(App).mount('#app')