在Vue中如何實現(xiàn)打字機的效果
前一段時間找工作做網(wǎng)頁簡歷,想實現(xiàn)打字機的效果。
按理說像打字機這種動畫效果的實現(xiàn)首選是jquery,畢竟jquery還是以操作dom為主,而vue是數(shù)據(jù)驅(qū)動,但是jquery并沒有能狗滿足我設想的功能的插件,又偷懶不想自己實現(xiàn)雙向綁定,只好自己用vue實現(xiàn)。
代碼寫的不是很漂亮,希望大家不喜勿噴,這里只是提供一個思路。
最終效果
I‘m ByPunk!
I‘m looking for a job.
I‘m a front-end programmer.
I‘m coding the web…
以上四句話循環(huán)重復
以打字機的效果顯示輸入和刪除,因為前面的I‘m是相同的,所以只對后面的內(nèi)容加以修改。
1.事先定義好字符串str:str='By Punk!',使用數(shù)組的splite方法將str分解成由字母組成的數(shù)組。
2.利用for循環(huán),每100毫秒向數(shù)組里push一個新的字母,利用vue的雙向綁定,數(shù)據(jù)更新帶動視圖更新。
3.在容納字母的div后寫一個豎杠“|”并加上不斷閃爍的動畫,模擬打字機光標。
4.在每次push的時候,判斷當前的索引i是否是數(shù)組的最后一個元素,如果是,則在2秒后開始清除。
5.“刪除”具體實現(xiàn)跟“輸入”剛好相反,每200毫秒從數(shù)組從pop出最后一項。
6.使用watch鉤子函數(shù)實現(xiàn)四句話的循環(huán)重復。
具體代碼如下
<template> ? ? <div class="typer"> ? ? ? <div class="typer-content"> ? ? ? ? <p class="typer-static">I'm </p> ? ? ? ? <!-- 動態(tài)變化的內(nèi)容--> ? ? ? ? <p class="typer-dynamic"> ? ? ? ? ? <span class="cut"> ? ? ? ? ? ? <span class="word" v-for="(letter,index) in words" :key="index">{{letter}}</span> ? ? ? ? ? </span> ? ? ? ? ? <!-- 模擬光標--> ? ? ? ? ? <span class="typer-cursor"></span> ? ? ? ? </p> ? ? ? </div> ? ? </div> </template>
<script> export default { ? data () { ? ? return { ? ? ? words:[], ? ? ? ? ? ? ? //字母數(shù)組push,pop的載體 ? ? ? str:"By Punk", ? ? ? ? ?//str初始化 ? ? ? letters:[], ? ? ? ? ? ? //str分解后的字母數(shù)組 ? ? ? order:1, ? ? ? ? ? ? ? ?//表示當前是第幾句話 ? ? } ? }, ? watch:{ ? ? ? ? ? ? ? ? ? ? //監(jiān)聽order值的變化,改變str的內(nèi)容 ? ? order(old,newV){ ? ? ? if(this.order%4==1){ ? ? ? ? this.str="By Punk!" ? ? ? } ? ? ? else if(this.order%4==2){ ? ? ? ? this.str="looking for a job. " ? ? ? } ? ? ? else if(this.order%4==3){ ? ? ? ? this.str="a front-end programmer." ? ? ? } ? ? ? else{ ? ? ? ? this.str="coding the web..." ? ? ? } ? ? } ? }, ? mounted(){ ? ? ? ? ? ?//頁面初次加載后調(diào)用begin()開始動畫 ? ? this.begin() ? }, ? methods:{ ? //開始輸入的效果動畫 ? ? begin(){ ? ? ? ? ? ? ? ? ? this.letters=this.str.split("") ? ? ? for(var i=0;i<this.letters.length;i++){ ? ? ? ? setTimeout(this.write(i),i*100); ? ? ? } ? ? }, ? //開始刪除的效果動畫 ? ? back(){ ? ? ? let L=this.letters.length; ? ? ? for(var i=0;i<L;i++){ ? ? ? ? setTimeout(this.wipe(i),i*50); ? ? ? } ? ? }, ? //輸入字母 ? ? write(i){ ? ? ? return ()=>{ ? ? ? ? ? let L=this.letters.length; ? ? ? ? ? this.words.push(this.letters[i]); ? ? ? ? ? let that=this; ? ? ? ? ? ?/*如果輸入完畢,在2s后開始刪除*/ ? ? ? ? ? if(i==L-1){? ? ? ? ? ? ? setTimeout(function(){ ? ? ? ? ? ? ? that.back(); ? ? ? ? ? ? },2000); ? ? ? ? ? } ? ? ? } ? ? }, ? //擦掉(刪除)字母 ? ? wipe(i){ ? ? ? return ()=>{ ? ? ? ? ? this.words.pop(this.letters[i]); ? ? ? ? ? /*如果刪除完畢,在300ms后開始輸入*/ ? ? ? ? ? if(this.words.length==0){ ? ? ? ? ? ? ?this.order++; ? ? ? ? ? ? ?let that=this; ? ? ? ? ? ? ?setTimeout(function(){ ? ? ? ? ? ? ? ?that.begin(); ? ? ? ? ? ? ?},300); ? ? ? ? ? } ? ? ? } ? ? }, ? }, } </script>
<style scoped lang="less"> @thePink:#e84d49; .typer{ ? margin-top: 2%; ? box-sizing: border-box; } .typer .typer-content{ ? font-weight: bold; ? font-size: 50px; ? display: flex; ? flex-direction: row; ? letter-spacing: 2px } .typer-dynamic{ ? position: relative; } .cut{ ? color: @thePink; } .typer-cursor{ ? position: absolute; ? height: 100%; ? width: 3px; ? top: 0; ? right: -10px; ? background-color: @thePink; ? animation: flash 1.5s linear infinite; } </style>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue用v-for給循環(huán)標簽自身屬性添加屬性值的方法
這篇文章主要介紹了Vue用v-for給循環(huán)標簽自身屬性添加屬性值的方法,文中大家給大家列舉了三種方法 ,需要的朋友可以參考下2018-10-10vue3+ElementPlus封裝函數(shù)式彈窗組件詳解
這篇文章主要為大家詳細介紹了如何利用vue3和ElementPlus封裝函數(shù)式彈窗組件,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-08-08Vue中使用v-print打印出現(xiàn)空白頁問題及解決
這篇文章主要介紹了Vue中使用v-print打印出現(xiàn)空白頁問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09vue自定義插件封裝,實現(xiàn)簡易的elementUi的Message和MessageBox的示例
這篇文章主要介紹了vue自定義插件封裝,實現(xiàn)簡易的elementUi的Message和MessageBox的示例,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-11-11