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>

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

相关推荐
逐光老顽童10 分钟前
用 Go 实现一个 LLM 路由网关:Thompson Sampling 与自适应故障转移实践
vue.js·go
wuhen_n13 分钟前
RAG 第一步:多格式文档加载与文本预处理实战
前端·langchain·ai编程
程序员黑豆1 小时前
全新系列开启:AI 全栈开发
前端·后端·全栈
小小小小宇1 小时前
Partial Clone
前端
小小小小宇1 小时前
git sparse-checkout(稀疏检出)
前端
ZC跨境爬虫1 小时前
跟着 MDN 学JavaScript day_9:字符串方法实战挑战与解题思路
开发语言·前端·javascript
夜焱辰1 小时前
WebMCP 的正确打开方式:只注册 2 个工具,代理 N 个——CreatorWeave 的 On-Demand 实践
前端
hewins2 小时前
NestJS 从入门到精通
javascript
柒和远方2 小时前
LeetCode 452. 用最少数量的箭引爆气球 —— 区间贪心经典:排序 + 扫描一箭穿心
javascript·python·算法
用户7459571748402 小时前
Fabric:Python SSH 远程执行利器
前端