vue实现文字手工动态打出效果

vue实现文字手工动态打出效果

问题背景

本文实现vue中,动态生成文字手动打出效果。

问题分析

话不多说,直接上代码:

复制代码
<template>
  <main>
    <button @click="makeText">
      <p class="text">点击生成内容</p>
    </button>

    <p class="text" :class="{'outer-border' : stringA.length > 0}">{{ stringA }}</p>
  </main>
</template>
 
<script>
export default {
  data() {
    return {
      stringB : '卜算子·咏梅\n毛泽东\n风雨送春归,\n飞雪迎春到。\n已是悬崖百丈冰,\n犹有花枝俏。\n俏也不争春,\n只把春来报。\n待到山花烂漫时,\n她在丛中笑。',
      stringA : '',
    }
  },
  components: {
  },
  methods: {
    makeText() {
      console.log(this.stringA.length, this.stringB.length);
      if (this.stringA.length >= this.stringB.length) {
        // 退出递归
        return;
      } else {
        // stringA仍然比stringB短,进入下一次等待和递归
        setTimeout(() => {
          this.stringA += this.stringB.charAt(this.stringA.length);
          this.makeText(this.stringA, this.stringB);
        }, 50);
      }
    }
  }
}
</script>

<style scoped>
.text {
  font-size: 30px;
  max-width: 800px;
}

.outer-border {
  border: 2px solid black;
  border-radius: 5px;
  padding: 3px;
  white-space: pre-wrap;
}
</style>

运行结果如下:

问题总结

本文实现了文字手工动态打出效果,有兴趣的同学可以进一步深入研究。

相关推荐
利刃大大12 小时前
【Vue】自定义指令directives && 指令钩子 && IntersectionObserver
前端·javascript·vue.js
共享家952718 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
摘星编程20 小时前
OpenHarmony环境下React Native:自定义useTruncate文本截断
javascript·react native·react.js
Duang007_20 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
有来技术20 小时前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东51621 小时前
学院个人信息管理系统 (springboot+vue)
vue.js·spring boot·后端·个人开发·毕设
2601_949868361 天前
Flutter for OpenHarmony 电子合同签署App实战 - 主入口实现
开发语言·javascript·flutter
m0_748229991 天前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
xiaoxue..1 天前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试
摘星编程1 天前
在OpenHarmony上用React Native:自定义useHighlight关键词高亮
javascript·react native·react.js