当前位置: 网站首页 >> 知识库 >> 正文
CSS 打字动画
发布时间:2025-12-01       编辑:网络中心       浏览次数:

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://yaochong.blog.csdn.net/article/details/121132542


打字动画

来源: 传送门


效果


代码


<!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>Document</title>

   <style>

       * {

           margin: 0;

           padding: 0px;

           box-sizing: border-box;

           font-family: consolas;

       }


       body {

           /* 使用弹性布局,实现居中 */

           display: flex;

           justify-content: center;

           align-items: center;

           min-height: 100vh;

           background: #121213;

       }


       .loader {

           position: relative;

           /* 文字的倒影效果 */

           -webkit-box-reflect: below 1px linear-gradient(transparent, #0002)

       }


       .loader h2 {

           position: relative;

           color: aliceblue;

           letter-spacing: 5px;


           /* 文字动画 ,动画的步数就是字符的个数*/

           animation: typing 8s steps(10) infinite;

           /* 超出的文字隐藏 */

           overflow: hidden;

       }


       /* 光标 */

       .loader::before {

           content: '';

           position: absolute;

           top: 0;

           right: 0;

           width: 2px;

           height: 100%;

           background: aliceblue;

           /* 动画 ,steps将动画分步,动画从0到100%经过3步*/

           animation: blinkCursor 0.8s steps(3) infinite;

       }


       /* 实现光标的闪烁效果 */

       @keyframes blinkCursor {


           0%,

           75% {

               opacity: 1;

           }


           76%,

           100% {

               opacity: 0;

           }

       }


       @keyframes typing {


           0%,

           90%,

           100% {

               width: 0;

           }


           30%,

           60% {

               /* 宽度是文字的宽度,通过控制台看出来的 */

               width: 181.95px;

           }

       }

   </style>

</head>


<body>

   <div>

       <h2>Loading...</h2>

   </div>

</body>


</html>


代码的核心是文字宽度的改变,超出的部分隐藏



下一条:CSS MASK

关闭本页

广东茂名农林科技职业学院教育技术与网络中心版权所有

© by GDAFC Education Technology & Network Center, All Rights Reserved.