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 分钟前
javascript之二重循环练习
开发语言·javascript·数据库
Mr Xu_25 分钟前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
一 乐1 小时前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
科技D人生1 小时前
Vue.js 学习总结(20)—— Vue-Office 实战:word、pdf、excel、ppt 多种文档的在线预览
vue.js·word·vue-pdf·stylesheet·docx-preview·vue-office
vx1_Biye_Design1 小时前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven
vx_Biye_Design1 小时前
基于Spring Boot+vue的湖北旅游景点门票预约平台的设计--毕设附源码29593
java·vue.js·spring boot·spring cloud·servlet·eclipse·课程设计
hedley(●'◡'●)1 小时前
基于cesium和vue的大疆司空模仿程序
前端·javascript·vue.js·python·typescript·无人机
qq5_8115175151 小时前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
百思可瑞教育1 小时前
构建自己的Vue UI组件库:从设计到发布
前端·javascript·vue.js·ui·百思可瑞教育·北京百思教育
百锦再1 小时前
Vue高阶知识:利用 defineModel 特性开发搜索组件组合
前端·vue.js·学习·flutter·typescript·前端框架