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>

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

相关推荐
Moment5 小时前
如果你在做 RAG,可能会需要 pdf-inspector
前端·后端·面试
gnip5 小时前
Flutter 原生插件开发实战指南
前端·flutter
FYKJ_20106 小时前
springboot羽毛球场地管理系统00626-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
晓得迷路了6 小时前
栗子前端技术周刊第 148 期 - Turborepo 2.11、Chrome 154 iframe、Node.js 26...
前端·javascript·css
IT_陈寒6 小时前
Vite热更新失效?你可能漏了这个配置项
前端·人工智能·后端
FYKJ_20106 小时前
django药饮个性化推荐系统54839-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
10年前端老司机7 小时前
Next.js+LangGraph.js+ 简历工具AI Agent完整落地
前端·langchain·agent
IT_陈寒12 小时前
Java中equals方法比了个寂寞?原来这才是正确的重写姿势
前端·人工智能·后端
默_笙12 小时前
🚓 分诊台与拆题术:让 RAG 学会判断和规划
前端·javascript
CopyCode12 小时前
用 AI 迁项目有多爽?我把 Webpack 迁 Vite 的全过程记下来了
前端·架构