vue实现打字效果
html:
<div class="home-main-dictum-info"> <span>{{ dictumInfo }}</span> <span class="home-main-typed-cursor" :class="{ 'is-typed-cursor-anmation': watingTyped }">|</span> </div>
js:
const dictums = ref([ ['没有任何动物比蚂蚁更勤奋,然而它却最沉默寡言。', '出自:富兰克林'], ['勤劳一日,可得一夜安眠;勤劳一生,可得幸福长眠。', '出自:达芬奇'], ['学会学习的人,是非常幸福的人。', '出自:米南德'], ['我们自动的读书,即嗜好的读书,请教别人是大抵无用,只好先行泛览,然后决择而入于自己所爱的较专的一门或几门;但专读书也有弊病,所以必须和现实社会接触,使所读的书活起来。', '出自:鲁迅'], ['各种蠢事,在每天阅读书的影响下,仿佛在火上一样,渐渐溶化。', '出自:雨果'], ]); // 打字文本 const dictumInfo = ref(''); const timer = ref(null); const watingTyped = ref(false); const backTimer = ref(null); async function startPlay() { const newDictums = dictums.value.flat(); const tasks = newDictums.map((dictum) => { return createTask(async (resolve) => { let i = 0; timer.value = setInterval(async () => { dictumInfo.value = dictum.substring(0, i + 1); i++; if (i >= dictum.length) { if (timer.value) { clearInterval(timer.value); watingTyped.value = true; await sleep(800); watingTyped.value = false; backTimer.value = setInterval(async () => { dictumInfo.value = dictum.substring(0, i); i--; if (i < 0) { watingTyped.value = true; await sleep(200); watingTyped.value = false; resolve(); if (backTimer.value) clearInterval(backTimer.value); } }, 100); } } }, 250); }); }); await tasks.reduce((pre, next) => pre.then((ret) => next(ret)), Promise.resolve()); startPlay(); } function createTask(cb) { return () => new Promise((resolve) => { cb(resolve); }); } function sleep(delay = 500) { return new Promise((resolve) => { setTimeout(resolve, delay); }); } startPlay()
scss:
.home-main-dictum-info { font-size: 24px; height: 200px; display: flex; flex-direction: row; align-items: center; justify-content: center; line-height: 40px; } .home-main-typed-cursor { margin-left: 4px; font-size: 28px; } .is-typed-cursor-anmation { animation: typed 0.5s ease infinite alternate; }
效果(无法发动图,只截了个图):
希望大佬看到有不对的地方,提出博主予以改正!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix