<div style="font-size: 0.1852rem;margin-top:1vh;">
{{ typedText }}
</div>
text: '这是一段文字',
index: 0,
typedText: '',
timer: null
typeText() {
if (this.index < this.text.length) {
this.typedText = this.text.substring(0, this.index++);
this.timer = setTimeout(this.typeText, 50); // 每0.2秒添加一个字符
}
},
在 mounted中调用typeText()
beforeDestroy() {
if (this.timer) {
clearTimeout(this.timer);
}
}