打字动画
灵感胜于汗水 Lv5

打字动画

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
html,
body {
margin: 0;
}
.color1 {
color: #e60012;
}
.color2 {
color: #eb6100;
}
.color3 {
color: #fcc800;
}
.color4 {
color: #22ac38;
}
.color5 {
color: #0086d1;
}
.color6 {
color: #1d2088;
}
.color7 {
color: #920783;
}
.word {
font-size: 20px;
}
.content {
text-align: center;
font-size: 0;
}
.blink {
font-size: 20px;
animation: fade 500ms infinite;
-webkit-animation: fade 500ms infinite;
}
@keyframes fade {
from {
opacity: 1;
}
50% {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>
</head>

<body>
<div class="content">
<span class="blink" id="jsBlink">|</span>
</div>
<script type="text/javascript">
function output(str) {
const contentDom = document.querySelector('.content')
contentDom.innerHTML = '<span class="blink" id="jsBlink">|</span>'
const blinkDom = document.querySelector('.blink')
const enterIndexs = []
while (str.indexOf('\n') > -1) {
enterIndexs.push(str.indexOf('\n') + enterIndexs.length)
str = str.replace('\n', '')
}
const arr = str.split('')
const doms = arr.map((item) => {
if (item === ' ') item = '&nbsp;'
else if (item === '>') item = '\>'
else if (item === '<') item = '\<'
const dom = document.createElement('span')
dom.innerHTML = item
dom.className = `word color${Math.ceil(Math.random() * 7)}`
return dom
})
enterIndexs.forEach((item) => {
const brDom = document.createElement('br')
doms.splice(item, 0, brDom)
})
console.log(contentDom)
console.log(blinkDom)
for (let i = 0; i < doms.length; i++) {
setTimeout(() => {
console.log(doms[i])
contentDom.insertBefore(doms[i], blinkDom)
}, 200 * (i + 1))
}
}
output('Hello world\n你好,世界!')
</script>
</body>
</html>
  • 本文标题:打字动画
  • 本文作者:灵感胜于汗水
  • 创建时间:2022-10-22 16:56:12
  • 本文链接:https://cjhsyc.github.io/2022/10/22/打字动画/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!