跳绳鸭
67.76M · 2026-02-04
打字机效果是一种常见的页面动画效果,它模拟了打字机逐字输入的过程,为静态页面增添了动态感和趣味性。在Vue 3中,我们可以通过组合式API、响应式数据和定时器来实现这一效果。
打字机效果的核心原理是:
<template>
<!-- 标题/标语区 -->
<div class="header">
<h1 class="main-title" style="min-height: 60px;">{{ typedTitle }}</h1>
<p class="sub-title">AI智能优化,让简历闪闪发光</p>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const typedTitle = ref('');
const fullTitle = '为你的工作经历画龙点睛';
// 页面加载完成后执行
onMounted(() => {
typeWriter();
});
// 打字机效果函数
function typeWriter() {
let typedText = '';
let index = 0;
const typeInterval = setInterval(() => {
if (index < fullTitle.length) {
typedText += fullTitle.charAt(index);
typedTitle.value = typedText;
index++;
} else {
clearInterval(typeInterval);
}
}, 150); // 每个字符的间隔时间,单位毫秒
}
</script>
<style scoped>
.main-title {
font-size: 32px;
font-weight: bold;
color: #333;
margin-bottom: 20px;
line-height: 1.3;
}
.sub-title {
font-size: 18px;
color: #666;
margin: 0;
}
</style>
循环展示的打字机效果包括以下阶段:
<template>
<!-- 标题/标语区 -->
<div class="header">
<h1 class="main-title" style="min-height: 60px;">{{ typedTitle }}</h1>
<p class="sub-title">AI智能优化,让简历闪闪发光</p>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const typedTitle = ref('');
const fullTitle = '为你的工作经历画龙点睛';
// 页面加载完成后执行
onMounted(() => {
typeWriter();
});
// 打字机效果函数 - 循环版
function typeWriter() {
let typedText = '';
let index = 0;
// 打字阶段
const typeInterval = setInterval(() => {
if (index < fullTitle.length) {
typedText += fullTitle.charAt(index);
typedTitle.value = typedText;
index++;
} else {
clearInterval(typeInterval);
// 打字完成后,等待2秒开始消失
setTimeout(() => {
fadeOutTitle();
}, 2000);
}
}, 150); // 每个字符的间隔时间,单位毫秒
}
// 标题淡出效果
function fadeOutTitle() {
let typedText = fullTitle;
let index = fullTitle.length - 1;
const fadeInterval = setInterval(() => {
if (index >= 0) {
typedText = typedText.substring(0, index);
typedTitle.value = typedText;
index--;
} else {
clearInterval(fadeInterval);
// 消失完成后,等待1秒重新开始
setTimeout(() => {
typeWriter();
}, 1000);
}
}, 100); // 每个字符的消失间隔时间,单位毫秒
}
</script>
<style scoped>
.main-title {
font-size: 32px;
font-weight: bold;
color: #333;
margin-bottom: 20px;
line-height: 1.3;
}
.sub-title {
font-size: 18px;
color: #666;
margin: 0;
}
</style>
clearInterval确保定时器在完成后被清除,避免内存泄漏min-height,避免打字过程中页面布局跳动打字机效果适用于以下场景:
以下是一个完整的Vue 3组件示例,包含了循环打字机效果的实现:
<template>
<div class="container">
<!-- 标题/标语区 -->
<div class="header">
<h1 class="main-title" style="min-height: 60px;">{{ typedTitle }}</h1>
<p class="sub-title">AI智能优化,让简历闪闪发光</p>
</div>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
const typedTitle = ref('');
const fullTitle = '为你的工作经历画龙点睛';
let typeInterval = null;
let fadeInterval = null;
// 页面加载完成后执行
onMounted(() => {
typeWriter();
});
// 组件卸载时清除定时器
onUnmounted(() => {
if (typeInterval) clearInterval(typeInterval);
if (fadeInterval) clearInterval(fadeInterval);
});
// 打字机效果函数 - 循环版
function typeWriter() {
let typedText = '';
let index = 0;
// 打字阶段
typeInterval = setInterval(() => {
if (index < fullTitle.length) {
typedText += fullTitle.charAt(index);
typedTitle.value = typedText;
index++;
} else {
clearInterval(typeInterval);
// 打字完成后,等待2秒开始消失
setTimeout(() => {
fadeOutTitle();
}, 2000);
}
}, 150); // 每个字符的间隔时间,单位毫秒
}
// 标题淡出效果
function fadeOutTitle() {
let typedText = fullTitle;
let index = fullTitle.length - 1;
fadeInterval = setInterval(() => {
if (index >= 0) {
typedText = typedText.substring(0, index);
typedTitle.value = typedText;
index--;
} else {
clearInterval(fadeInterval);
// 消失完成后,等待1秒重新开始
setTimeout(() => {
typeWriter();
}, 1000);
}
}, 100); // 每个字符的消失间隔时间,单位毫秒
}
</script>
<style scoped>
.container {
display: flex;
flex-direction: column;
padding: 40px 30px 60px;
min-height: 100vh;
background: linear-gradient(135deg, #f0f9ff 0%, #e6f7ff 100%);
}
.header {
text-align: center;
margin-bottom: 60px;
}
.main-title {
font-size: 32px;
font-weight: bold;
color: #333;
margin-bottom: 20px;
line-height: 1.3;
}
.sub-title {
font-size: 18px;
color: #666;
margin: 0;
}
</style>
如果需要在多个地方使用打字机效果,可以将其封装为一个可复用的组件:
<template>
<div class="typewriter-container">
<slot :text="typedText">{{ typedText }}</slot>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue';
const props = defineProps({
text: {
type: String,
default: '为你的工作经历画龙点睛'
},
speed: {
type: Number,
default: 150
},
loop: {
type: Boolean,
default: false
},
delay: {
type: Number,
default: 2000
}
});
const typedText = ref('');
let typeInterval = null;
let fadeInterval = null;
// 文本变化
watch(() => props.text, () => {
if (typeInterval) clearInterval(typeInterval);
if (fadeInterval) clearInterval(fadeInterval);
typedText.value = '';
typeWriter();
});
// 页面加载完成后执行
onMounted(() => {
typeWriter();
});
// 组件卸载时清除定时器
onUnmounted(() => {
if (typeInterval) clearInterval(typeInterval);
if (fadeInterval) clearInterval(fadeInterval);
});
// 打字机效果函数
function typeWriter() {
let text = '';
let index = 0;
// 打字阶段
typeInterval = setInterval(() => {
if (index < props.text.length) {
text += props.text.charAt(index);
typedText.value = text;
index++;
} else {
clearInterval(typeInterval);
if (props.loop) {
// 打字完成后,等待指定时间开始消失
setTimeout(() => {
fadeOutTitle();
}, props.delay);
}
}
}, props.speed);
}
// 标题淡出效果
function fadeOutTitle() {
let text = props.text;
let index = props.text.length - 1;
fadeInterval = setInterval(() => {
if (index >= 0) {
text = text.substring(0, index);
typedText.value = text;
index--;
} else {
clearInterval(fadeInterval);
// 消失完成后,等待1秒重新开始
setTimeout(() => {
typeWriter();
}, 1000);
}
}, props.speed * 0.6); // 消失速度稍快
}
</script>
<style scoped>
.typewriter-container {
/* 可根据需要添加样式 */
}
</style>
使用示例:
<template>
<div class="container">
<h1 class="main-title" style="min-height: 60px;">
<Typewriter
text="为你的工作经历画龙点睛"
:loop="true"
:speed="150"
/>
</h1>
<p class="sub-title">AI智能优化,让简历闪闪发光</p>
</div>
</template>
<script setup>
import Typewriter from './components/Typewriter.vue';
</script>
打字机效果是一种简单但有效的页面动画技术,通过JavaScript定时器和小程序的数据绑定机制,我们可以轻松实现这一效果。无论是单次显示还是循环展示,都可以为页面增添动态感和趣味性,提升用户体验。
在实现过程中,我们需要注意性能优化和用户体验,合理设置动画参数,确保效果流畅自然。同时,我们也可以根据具体场景进行扩展和定制,创造出更加丰富多样的动态效果。
希望本指南对你有所帮助,祝你在微信小程序开发中创造出更多精彩的页面效果**!**