2
2
<div class =" OuterMain" >
3
3
<div class =" inner" >
4
4
<div class =" sentence" @click =" typeText" >
5
- {{ displayText }}<span class =" cursor" v-if =" isTyping" >|</span >
5
+ {{ displayText }}
6
+ <span class =" cursor" v-if =" isTyping" >|</span >
6
7
</div >
7
8
<div class =" source" >
8
- {{ sentence ? sentence.author : "" }}
9
- {{ sentence ? `(${sentence.name})` : "" }}
9
+ {{ sentence ? sentence.author : '' }}
10
+ {{ sentence ? `(${sentence.name})` : '' }}
10
11
</div >
11
12
</div >
12
13
</div >
13
14
</template >
14
15
15
16
<script setup>
16
17
const sentence = ref ()
17
- const displayText = ref (" " )
18
- const fullText = ref (" " )
18
+ const displayText = ref (' ' )
19
+ const fullText = ref (' ' )
19
20
const isTyping = ref (false )
20
21
21
22
// 打字效果函数
22
23
const typeText = async () => {
23
24
isTyping .value = true
24
- displayText .value = " "
25
+ displayText .value = ' '
25
26
26
27
for (let i = 0 ; i < fullText .value .length ; i++ ) {
27
28
if (! isTyping .value ) break
@@ -37,27 +38,31 @@ const typeText = async () => {
37
38
const { data } = await useAsyncData (
38
39
` sentence-${ Math .floor (Date .now () / 60000 )} ` ,
39
40
async () => {
40
- const res = await $fetch (" https://open.saintic.com/api/sentence/" )
41
+ const res = await $fetch (' https://open.saintic.com/api/sentence/' )
41
42
return {
42
- sentence: res? .data ? .sentence || " " ,
43
- author: res? .data ? .author || " " ,
44
- name: res? .data ? .name || " " ,
43
+ sentence: res? .data ? .sentence || ' ' ,
44
+ author: res? .data ? .author || ' ' ,
45
+ name: res? .data ? .name || ' ' ,
45
46
}
46
47
},
47
48
{
48
49
default : () => null ,
49
- server: false
50
- }
50
+ server: false ,
51
+ },
51
52
)
52
53
53
54
// 监听数据变化并执行打字效果
54
- watch (data, async (newData ) => {
55
- if (newData? .sentence ) {
56
- sentence .value = newData
57
- fullText .value = newData .sentence
58
- await typeText ()
59
- }
60
- }, { immediate: true })
55
+ watch (
56
+ data,
57
+ async (newData ) => {
58
+ if (newData? .sentence ) {
59
+ sentence .value = newData
60
+ fullText .value = newData .sentence
61
+ await typeText ()
62
+ }
63
+ },
64
+ { immediate: true },
65
+ )
61
66
62
67
// 组件卸载时停止打字
63
68
onBeforeUnmount (() => {
@@ -74,7 +79,7 @@ onBeforeUnmount(() => {
74
79
height: 45vh ;
75
80
background: rgb (var (-- mdui- color- surface- container- highest));
76
81
transform: translateY (calc (- 1 * var (-- inline- padding)));
77
-
82
+
78
83
.inner {
79
84
width: 70 % ;
80
85
min- width: 370px ;
@@ -84,7 +89,7 @@ onBeforeUnmount(() => {
84
89
flex- direction: column;
85
90
align- items: center;
86
91
box- sizing: border- box;
87
-
92
+
88
93
.sentence {
89
94
font- size: 1 .5rem ;
90
95
line- height: 1.6 ;
@@ -94,13 +99,13 @@ onBeforeUnmount(() => {
94
99
text- align: center;
95
100
font- weight: bold;
96
101
}
97
-
102
+
98
103
.source {
99
104
font- size: 1rem ;
100
105
opacity: 0.7 ;
101
106
text- align: center;
102
107
}
103
-
108
+
104
109
.cursor {
105
110
animation: blink 1s infinite;
106
111
color: var (-- mdui- color- primary);
@@ -109,7 +114,13 @@ onBeforeUnmount(() => {
109
114
}
110
115
111
116
@keyframes blink {
112
- 0 % , 50 % { opacity: 1 ; }
113
- 51 % , 100 % { opacity: 0 ; }
117
+ 0 % ,
118
+ 50 % {
119
+ opacity: 1 ;
120
+ }
121
+ 51 % ,
122
+ 100 % {
123
+ opacity: 0 ;
124
+ }
114
125
}
115
126
< / style>
0 commit comments