Vue實(shí)現(xiàn)半自動(dòng)打字機(jī)特效
最近遇到一個(gè)需求就是使用Web網(wǎng)頁(yè)實(shí)現(xiàn)打字機(jī)特效,網(wǎng)上搜了一下相關(guān)插件,發(fā)現(xiàn)不完全符合我們的需求,于是手寫實(shí)現(xiàn)一個(gè)半自動(dòng)的打字機(jī)。
1、源碼如下
<template>
<div class="type-writer">
<p class="type-writer-line">{{ line_1.typewriter }}<i class="type-writer-cursor" v-if="line_1.visible" /></p>
<p class="type-writer-line">{{ line_2.typewriter }}<i class="type-writer-cursor" v-if="line_2.visible" /></p>
<p class="type-writer-line">{{ line_3.typewriter }}<i class="type-writer-cursor" v-if="line_3.visible" /></p>
<p class="type-writer-line">{{ line_4.typewriter }}<i class="type-writer-cursor" v-if="line_4.visible" /></p>
</div>
</template>
<script>
export default {
data() {
return {
// 第一行文字
line_1: {
content: `git add '我的2023年度開(kāi)端'`,
typewriter: ``,
index: 0,
visible: false,
timer: null
},
// 第二行文字
line_2: {
content: `git commit -m 'Hello,2023!'`,
typewriter: ``,
index: 0,
visible: false,
timer: null
},
// 第三行文字
line_3: {
content: `git pull`,
typewriter: ``,
index: 0,
visible: false,
timer: null
},
// 第四行文字
line_4: {
content: `git push`,
typewriter: ``,
index: 0,
visible: false,
timer: null
},
};
},
mounted() {
this.typingLine_1(this.line_1)
},
methods: {
/**
* 打印第一行文字
*/
typingLine_1(obj) {
obj.visible = true
if (obj.index <= obj.content.length) {
obj.typewriter = obj.content.slice(0, obj.index ++)
obj.timer = setTimeout(() => { this.typingLine_1(obj) }, 50)
} else {
clearTimeout(obj.timer)
obj.visible = false
this.typingLine_2(this.line_2);
}
},
/**
* 打印第二行文字
*/
typingLine_2(obj) {
obj.visible = true
if (obj.index <= obj.content.length) {
obj.typewriter = obj.content.slice(0, obj.index ++)
obj.timer = setTimeout(() => { this.typingLine_2(obj) }, 50)
} else {
clearTimeout(obj.timer)
obj.visible = false
this.typingLine_3(this.line_3);
}
},
/**
* 打印第三行文字
*/
typingLine_3(obj) {
obj.visible = true
if (obj.index <= obj.content.length) {
obj.typewriter = obj.content.slice(0, obj.index ++)
obj.timer = setTimeout(() => { this.typingLine_3(obj) }, 50)
} else {
clearTimeout(obj.timer)
obj.visible = false
this.typingLine_4(this.line_4);
}
},
/**
* 打印第四行文字
*/
typingLine_4(obj) {
obj.visible = true
if (obj.index <= obj.content.length) {
obj.typewriter = obj.content.slice(0, obj.index ++)
obj.timer = setTimeout(() => { this.typingLine_4(obj) }, 50)
} else {
clearTimeout(obj.timer)
}
},
},
};
</script>
<style lang="less" scoped>
.type-writer {
width: auto;
padding: 100px;
background-color: #f9f2e1;
p {
height: 20px;
line-height: 20px;
font-size: 20px;
margin: 7.5px 0;
padding: 5px;
background-color: #f5ecd7;
position: relative;
color: rgb(96, 109, 121);
font-family: '楷體';
}
.type-writer-cursor {
width: 0px;
height: 100%;
border-left: 2px solid transparent;
animation: typing 3s steps(16) forwards, cursor 1s infinite;
-webkit-animation: typing 3s steps(16) forwards, cursor 1s infinite;
}
}
/* animation */
@keyframes typing {
from {
width: 100%;
}
to {
width: 0;
}
}
@keyframes cursor {
50% {
border-color: #5e7ce0;
}
}
@-webkit-keyframes typing {
from {
width: 100%;
}
to {
width: 0;
}
}
@-webkit-keyframes cursor {
50% {
border-color: #5e7ce0;
}
}
/* / animation */
</style>2、效果如下

到此這篇關(guān)于Vue實(shí)現(xiàn)半自動(dòng)打字機(jī)特效的文章就介紹到這了,更多相關(guān)Vue 半自動(dòng)打字機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+ElementUI前端添加展開(kāi)收起搜索框按鈕完整示例
最近一直在用element ui做后臺(tái)項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Vue+ElementUI前端添加展開(kāi)收起搜索框按鈕的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
關(guān)于vue利用postcss-pxtorem進(jìn)行移動(dòng)端適配的問(wèn)題
這篇文章主要介紹了關(guān)于vue利用postcss-pxtorem進(jìn)行移動(dòng)端適配的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue3.0 vant popup渲染不出來(lái)問(wèn)題及解決
這篇文章主要介紹了vue3.0 vant popup渲染不出來(lái)問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue實(shí)現(xiàn)一拉到底的滑動(dòng)驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了vue簡(jiǎn)單的一拉到底的滑動(dòng)驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
VUE+Canvas實(shí)現(xiàn)簡(jiǎn)單五子棋游戲的全過(guò)程
這篇文章主要給大家介紹了關(guān)于VUE+Canvas實(shí)現(xiàn)簡(jiǎn)單五子棋游戲的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Vue.directive 實(shí)現(xiàn)元素scroll邏輯復(fù)用
這篇文章主要介紹了Vue.directive 實(shí)現(xiàn)元素scroll邏輯復(fù)用功能,文中給大家提到元素實(shí)現(xiàn)滾動(dòng)的條件有兩個(gè),具體內(nèi)容詳情大家參考下本文2019-11-11

