CSS打字效果是一种通过CSS动画模拟打字机打字效果的视觉效果。它通常用于标题、引言或其他需要吸引用户注意力的文本元素上。这种效果可以增加页面的动态感和交互性。
以下是一个简单的CSS打字效果的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Typing Effect</title>
<style>
.typing-effect {
font-size: 2em;
overflow: hidden; /* 隐藏溢出的文本 */
border-right: .15em solid orange; /* 光标效果 */
white-space: nowrap; /* 防止文本换行 */
letter-spacing: .15em; /* 字母间距 */
animation:
typing 3.5s steps(40, end), /* 打字动画 */
blink-caret .75s step-end infinite; /* 光标闪烁动画 */
}
/* 打字动画 */
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
/* 光标闪烁动画 */
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: orange }
}
</style>
</head>
<body>
<h1 class="typing-effect">Hello, World!</h1>
</body>
</html>
animation-duration
属性的值,以改变动画的速度。border-right
属性中的颜色值,以更改光标的颜色。overflow: hidden
和white-space: nowrap
,以防止文本溢出。通过以上方法,你可以轻松实现一个CSS打字效果,并根据需要进行调整和优化。
领取专属 10元无门槛券
手把手带您无忧上云