打字机效果——文字逐字出现、末尾光标闪烁。以前要做这个效果得上 JS,纯 CSS 其实也能实现。两种路线的代码各记一份,可直接使用。
纯 CSS:三层结构
把动画拆成三层:底层静止的文字、中间遮挡文字的背景、最上层的光标。初始时背景盖住全部文字、光标在最左;动画期间背景和光标以相同步伐从左往右移动;结束时背景不再遮挡文字,光标停在最右边闪烁。
![图片[1]-HTML/CSS/JS实现打字效果-本站](https://i1.img.azhai.de/v3/151662fc29d710a0.gif)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Typing effect without js</title>
<style>
:root {
/* number of characters */
--steps: 345;
/* animation time */
--duration: 2.5s;
--fontSize: 50px;
--cursorSize: 20px;
}
.text {
color: #333;;
position: relative;
display: inline-block;
font-family: 'Courier New', Courier, monospace;
font-size: var(--fontSize);
line-height: 1;
}
.text::after {
content: '';
width: var(--cursorSize);
height: var(--fontSize);
background-color: black;
z-index: 2;
position: absolute;
animation: blink 1s var(--duration) step-end infinite,
moveCursor var(--duration) steps(var(--steps)) forwards;
}
.text::before {
content: '';
width: 100%;
height: var(--fontSize);
z-index: 1;
position: absolute;
background: linear-gradient(#fff, #fff) no-repeat top right;
animation: showText var(--duration) steps(var(--steps)) forwards;
}
/* Cursor blink animation */
@keyframes blink {
0% {
background-color: black;
}
50% {
background-color: transparent;
}
100% {
background-color: black;
}
}
/* Cursor movement animation */
@keyframes moveCursor {
0% {
left: 0%;
}
100% {
left: 100%;
}
}
/* background moving animation */
@keyframes showText {
0% {
background-size: 100% 100%;
}
100% {
background-size: 0% 100%;
}
}
</style>
</head>
<body>
<div class="text">hello,world!</div>
</body>
</htm>
参数集中在 :root:–steps 是字符数、–duration 是动画时长,光标和文字尺寸各一个变量。文字内容改动后要同步改 –steps,否则背景移动步数和字符数对不齐。
这套方案唯一的好处是不需要 JS;缺点是目前只适合一行文本,多行理论上能实现但很麻烦。
JS 写法一:setTimeout 逐字追加
![图片[2]-HTML/CSS/JS实现打字效果-本站](https://i1.img.azhai.de/v3/0f009da1b4ab804a.gif)
完整字符串存 JS 变量,每 200ms 用 slice 截取递增长度写回 div,末尾拼一个下划线当光标:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Typing effect with js</title>
</head>
<body>
<div style="font-size: 36px;" id="text">hello,world!</div>
<script>
var str = 'In the past, to achieve an effect similar to typing on a computer,js+html was required. Today I will introduce a new method.';
var i = 0;
function typing(){
var divTyping = document.getElementById('text');
if (i <= str.length) {
divTyping.innerHTML = str.slice(0, i++) + '_';
setTimeout('typing()', 200);
}
else{
divTyping.innerHTML = str;
}
}
typing();
</script>
</body>
</html>
JS 写法二:隐藏元素 + setInterval + substring
![图片[2]-HTML/CSS/JS实现打字效果-本站](https://i1.img.azhai.de/v3/0f009da1b4ab804a.gif)
思路相同,只是把原文藏在另一个隐藏 div 里,index 递增配合 setInterval 用 substring 截取:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Typing effect with js</title>
<style>
.divcss5{word-wrap:break-word;font-size: 36px;}
</style>
</head>
<body>
<div class="divcss5" id="aa">
In the past, to achieve an effect similar to typing on a computer, js+html was required. Today I will introduce a new method.This article mainly
</div>
<div style="display:none" id="w">
In the past, to achieve an effect similar to typing on a computer, js+html was required. Today I will introduce a new method.This article mainly introduces pure html+css to achieve typing effect, which has certain reference value, you can learn about it. Provide all the code, you can use it directly.
</div>
<script language="javascript">
var index=0;
var word=document.getElementById("w").innerHTML;
function type(){
document.getElementById("aa").innerText = word.substring(0,index++);
}
setInterval(type, 200);
</script>
</body>
</html>
怎么选
单行标题、固定文案,CSS 方案干净、零脚本开销;文案长、动态从后端取,或要多行排版,老老实实用 JS。另外 CSS 方案的 steps() 按字符数走步,中英混排时宽度对不齐的情况,上线前实际看一眼。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















暂无评论内容