vue 动画加载文字 像打字机那样的
Tue Jan 09 2024 12:13:12 GMT+0000 (Coordinated Universal Time)
Saved by
@yangxudong
<template>
<div>
<div class="typing-text" v-if="showText">{{ currentText }}</div>
</div>
</template>
<script>
export default {
data() {
return {
textToType: "Hello, World!", // 你想要加载的文字
currentText: "", // 当前显示的文字
showText: true // 控制是否显示文字
};
},
methods: {
typeText() {
const textArray = this.textToType.split("");
let index = 0;
const typingInterval = setInterval(() => {
this.currentText += textArray[index];
index++;
if (index === textArray.length) {
clearInterval(typingInterval);
// 在此可以触发加载完成后的操作
}
}, 100); // 调整间隔时间
}
},
mounted() {
this.typeText();
}
};
</script>
<style>
.typing-text {
font-size: 24px;
font-weight: bold;
display: inline-block;
border-right: 2px solid; /* 光标效果,可以根据需要调整 */
animation: cursor-blink 1s infinite; /* 光标闪烁动画,可以根据需要调整 */
}
@keyframes cursor-blink {
0%, 100% {
border-color: transparent;
}
50% {
border-color: #000; /* 光标颜色,可以根据需要调整 */
}
}
</style>
content_copyCOPY
https://chat.openai.com/c/5d144265-cb1f-494c-a6c8-920bba113851
Comments