24 行
537 B
TypeScript
24 行
537 B
TypeScript
// 文字打字机效果
|
|
export const typewriterEffect = (selectors: string) => {
|
|
const typedTextContainer = document.querySelector<HTMLDivElement>(selectors)
|
|
|
|
if (!typedTextContainer) return
|
|
|
|
const text = typedTextContainer.innerText
|
|
|
|
// 清除原有的文本
|
|
typedTextContainer.innerText = ''
|
|
|
|
// 实现打字机效果
|
|
let i = 0
|
|
|
|
const typewriter = () => {
|
|
if (i >= text.length) return
|
|
|
|
typedTextContainer.innerText += text.charAt(i++)
|
|
|
|
setTimeout(typewriter, Math.random() * 200 + 50)
|
|
}
|
|
|
|
typewriter()
|
|
} |