vue3打字机效果

<template>
  <div>
    <p>{{ text }}</p>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
const text = ref(""); // 要显示的文字
const speed = 100; // 打字速度,单位:毫秒
let index = 0;
const originalText =
  "前端可以使用axios库来处理eventStream数据。axios提供了一个**request**方法来发送HTTP请求,并且可以通过配置参数来处理不同类型的请求。";

const typeWriter = () => {
  if (index < originalText.length) {
    text.value += originalText.charAt(index);
    index++;
    setTimeout(typeWriter, speed);
  }
};

onMounted(() => {
  typeWriter(); // 调用打字机函数
});
</script>

<style lang="scss" scoped>
</style>

 

posted @ 2024-05-22 17:37  xuanPhoto  阅读(611)  评论(0)    收藏  举报