利用动态 max-width
实现文本展开效果
实现代码
<template>
<abbr data-title="Animation Comics Games" ref="abbr">ACG</abbr>
</template>
<script setup>
import { ref, onMounted, useCssModule } from 'vue';
const style = useCssModule();
const abbr = ref(null);
onMounted(() => {
abbr.value.textContent = '';
let title = abbr.value.dataset.title;
let words = title.split(' ');
words.forEach((word) => {
let [initial, ...restLetters] = word.split('');
let initialSpan = document.createElement('span');
initialSpan.textContent = initial;
initialSpan.className = style.initial;
abbr.value.append(initialSpan);
restLetters.forEach((letter) => {
let hiddenSpan = document.createElement('span');
hiddenSpan.textContent = letter;
hiddenSpan.className = style.hidden;
abbr.value.append(hiddenSpan);
});
});
});
</script>
<style lang="scss" module>
abbr {
display: flex;
color: white;
font-size: 2em;
font-weight: bold;
font-family: Lato, sans-serif;
text-decoration: none;
span {
text-decoration: underline white;
transition: all 0.5s ease-in-out;
&.hidden {
max-width: 0;
text-decoration: none;
opacity: 0;
}
}
&:hover {
span {
max-width: 2em;
text-decoration: none;
opacity: 1;
&.initial {
margin-left: 0.5em;
}
}
}
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64