前端实现流式显示文本

<template>
  <div>
    <h1>{{ typedText }}</h1>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const text = 'Hello, World!';
const typedText = ref('');

onMounted(() => {
  let index = 0;
  const interval = setInterval(() => {
    typedText.value += text[index];
    index++;
    if (index === text.length) {
      clearInterval(interval);
    }
  }, 100);
});
</script>

 

posted @ 2023-11-15 15:37  焦廉琨  阅读(358)  评论(0编辑  收藏  举报