以前要达到类似在电脑上打字的效果,需要 js+html。今天我将介绍一种新方法。本文主要介绍纯 html+css 实现打字效果,有一定的参考价值,大家可以学习一下。提供所有代码,可以直接使用。

一、原理分析

可以将动画看做三个不同的层次:

1.最底层的文字

2.中间挡住文字的背景

3.最上层的光标

文字是静止的,而中间的背景和最上层的光标是动态的。

初始时,背景挡住所有的文字,光标在最左边。

动画进行时,背景和光标以相同的步伐从左往右移动。

动画结束时,背景不再遮挡文字,光标则在最右边闪烁。

这种实现唯一的好处是不需要 js,但缺点是只能用于一行文本,多行文本理论上可以实现,但是比较麻烦。

<!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>

最新文章

  1. Linux更改主机名--适用于Centos
  2. 安卓 9.png 图片的制作
  3. BZOJ2061 : Country
  4. atitit. 统计功能框架的最佳实践(1)---- on hibernate criteria
  5. ytu 2030: 求实数绝对值(水题)
  6. thinkPHP3.2.3完整版 在sae上面的部署
  7. line-height的小技巧
  8. 使用Echarts的五个步骤
  9. MYSQL 执行计划
  10. Android中网络流量控制(防火墙)——Iptables
  11. React学习系列
  12. jQuery插件ImgAreaSelect 实例讲解一(头像上传预览和裁剪功能)
  13. Django ORM模型:想说爱你不容易
  14. WPFの数据源事件变更通知
  15. Linux集群部署自定义时间同步服务器(ntpd)
  16. Java Runnable与Callable区别
  17. windows下mysql配置(第一次)
  18. 分别通过【buildpath】和【lib】倒入JAR包有什么不同
  19. hdu 3474 Necklace 单调队列
  20. String类使用

热门文章

  1. c++ 超长整数减法 高精度减法
  2. Swoole一键操作基于阿里云的RDS数据库迁移+OSS文件搬迁
  3. JVM 输出 GC 日志导致 JVM 卡住,我 TM 人傻了
  4. Centos 创建新的虚拟环境
  5. 2022年最强八股文《码出八股文-斩出offer线》
  6. 一图读懂k8s informer client-go
  7. RPA应用场景-考勤审批
  8. HTML:&lt;input type=&quot;text&quot;&gt; 输入数字时的验证!(在提交时验证)
  9. Lepton 无损压缩原理及性能分析
  10. 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据