Vue3实现打字机效果

typeit 介绍

typeit是一款轻量级打字机特效插件。该打印机特效可以设置打字速度,是否显示光标,是否换行和延迟时间等属性,它可以打印单行文本和多行文本,并具有可缩放、响应式等特点。官方文档

安装

# npm
npm install typeit
# pnpm
pnpm add typeit 

代码

<template>
    <span ref="text" class="msg"></span>
</template>
    
<script setup>
// https://www.typeitjs.com/docs/vanilla/usage/#configuration-basics
// npm install typeit
import { ref, onMounted } from "vue";
import TypeIt from 'typeit'
const text = ref(null)
onMounted(() => {
    new (TypeIt)(text.value, {
        strings: ["你好", "欢迎", "测试测试测试"],
        cursorChar: "<span class='cursorChar'>|<span>",//用于光标的字符。HTML也可以
        speed: 100,
        lifeLike: true,// 使打字速度不规则
        cursor: true,//在字符串末尾显示闪烁的光标
        breakLines: false,// 控制是将多个字符串打印在彼此之上,还是删除这些字符串并相互替换
        loop: true,//是否循环
    }).go()
})
</script>
    
<style scoped>
.msg {
    color: rgb(88, 88, 88);
    letter-spacing: 2px;
}

.msg :v-deep(.cursorChar){
    display: inline-block;
    margin-left: 2px;
}
</style>    

封装为组件

<template>
    <span ref="text" class="msg"></span>
</template>
    
<script setup>
/**
 * 打字机效果
 * npm install typeit
 * https://www.typeitjs.com/docs/vanilla/usage/#configuration-basics
 */
import { ref, onMounted } from "vue";
import TypeIt from 'typeit'
const props = defineProps({ strings: { type:Array } })
const text = ref(null)
onMounted(() => {
    new (TypeIt)(text.value, {
        strings: props.text,
        cursorChar: "<span class='cursorChar'>|<span>",//用于光标的字符。HTML也可以
        speed: 150,
        lifeLike: true,// 使打字速度不规则
        cursor: true,//在字符串末尾显示闪烁的光标
        breakLines: false,// 控制是将多个字符串打印在彼此之上,还是删除这些字符串并相互替换
        loop: true,//是否循环
    }).go()
})
</script>
    
<style lang='scss' scoped>
.msg {
    color: var(--el-color-info-light-3);
    letter-spacing: 2px;

    :v-deep(.cursorChar) {
        display: inline-block;
        margin-left: 2px;
    }
}
</style>
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级小的大杯柠檬水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值