vue实现自动打字效果(带光标效果)

代码介绍(其实就是通过字符串截取加定时拼接完成的,我相信有时间都能琢磨出来,来这里就是为了省事)

上vue页面代码:

javascript 复制代码
<template>
  <div id='App'>
    <h2>{{text}}<span ref="fou" class="fousdis">{{'_'}}</span></h2>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      initialText: '这是一个打字教程打字教程,。,。,777',
      index: 0,
      text: ''
    }
  },
  mounted() {
    setInterval(() => {
      this.autoTyping()
      var tim = setInterval(() => {
              this.$refs.fou.classList.toggle('fousdis')
               if (this.index === this.text.length) {
                    clearInterval(tim)
                      }
            }, 150)
    }, 300)
  },
  methods: {
    autoTyping() {
      this.index++
      this.text = this.initialText.slice(0, this.index)
      // this.index = this.text.length === this.initialText.length ? 0 : this.index  //实现循环打字
    }
  }
}
</script>
<style >
.fousdis{
  display: none;
}
</style>

老规矩:复制粘贴那去看效果

相关推荐
szephyr7 分钟前
前端错误监控实战:用 Sentry 把线上报错从“用户反馈“变成“主动发现“
前端·sentry·稳定性·前端监控·错误监控
拖孩14 分钟前
给小程序加「一键发公众号贴图」,我把 wx.shareToOfficialAccount 的三个坑趟了一遍
前端·后端·微信小程序
ONLYOFFICE19 分钟前
团队协作软件指南:主要类型、核心功能、选型建议等
前端·onlyoffice·office
志尊宝1 小时前
Vue3 零基础每日笔记(048):嵌套路由与命名视图——后台管理系统布局雏形
前端·javascript·vue.js·笔记·html5
Mr.Meng_951 小时前
彻底搞懂Promise.allSettled:用法、场景、源码手写、面试题详解
前端
心易行者1 小时前
Python在线运行+SQLite数据库实战:0成本搭个人数据后台,5个场景直接套用
前端·网络·人工智能·python
雪芽蓝域zzs1 小时前
第 10 章:大屏看板自动轮播高亮(ECharts 大屏经典效果)
vue.js·echars
@tangguo1232 小时前
npm 和 yarn 配置说明
前端·javascript·npm·node.js·yarn
zhiyouTech2 小时前
GEO实战:把生成式引擎优化当成检索工程来做(含五断点诊断与50题Benchmark)
前端·人工智能