经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
纯html+css实现打字效果_HTML/Xhtml
来源:jb51  时间:2021/8/4 9:11:14  对本文有异议

本文主要介绍了纯html+css实现打字效果,具有一定的参考价值,感兴趣的可以了解一下

效果图

分析
 

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

  • 最底层的文字
  • 中间挡住文字的背景
  • 最上层的光标

文字是静止的,而中间的背景和最上层的光标是动态的。
初始时,背景挡住所有的文字,光标在最左边。
动画进行时,背景和光标以相同的步伐从左往右移动。
动画结束时,背景不再遮挡文字,光标则在最右边闪烁。

代码
 

html
 

  1. <div class="text">hello,world!</div>

css
 

  1. :root {
  2. /* 字符数量 */
  3. --steps: 12;
  4. /* 动画时间 */
  5. --duration: 2.5s;
  6. /* 字体大小 */
  7. --fontSize: 50px;
  8. /* 光标大小 */
  9. --cursorSize: 20px;
  10. }
  11.  
  12. .text {
  13. color: #333;;
  14. position: relative;
  15. display: inline-block;
  16. font-family: 'Courier New', Courier, monospace;
  17. font-size: var(--fontSize);
  18. line-height: 1;
  19. }
  20.  
  21. .text::after {
  22. content: '';
  23. width: var(--cursorSize);
  24. height: var(--fontSize);
  25. background-color: black;
  26. z-index: 2;
  27. position: absolute;
  28. animation: blink 1s var(--duration) step-end infinite,
  29. moveCursor var(--duration) steps(var(--steps)) forwards;
  30. }
  31.  
  32. .text::before {
  33. content: '';
  34. width: 100%;
  35. height: var(--fontSize);
  36. z-index: 1;
  37. position: absolute;
  38. background: linear-gradient(#fff, #fff) no-repeat top right;
  39. animation: showText var(--duration) steps(var(--steps)) forwards;
  40. }
  41.  
  42. /* 光标闪烁动画 */
  43. @keyframes blink {
  44. 0% {
  45. background-color: black;
  46. }
  47. 50% {
  48. background-color: transparent;
  49. }
  50. 100% {
  51. background-color: black;
  52. }
  53. }
  54.  
  55. /* 光标移动动画 */
  56. @keyframes moveCursor {
  57. 0% {
  58. left: 0%;
  59. }
  60. 100% {
  61. left: 100%;
  62. }
  63. }
  64.  
  65. /* 背景移动动画 */
  66. @keyframes showText {
  67. 0% {
  68. background-size: 100% 100%;
  69. }
  70. 100% {
  71. background-size: 0% 100%;
  72. }
  73. }
  74.  

注意
字体必须是等宽字体。因为光标每次移动的距离是是根据字符的数量 / 总宽度来决定的。
 

在线演示
 

到此这篇关于纯html+css实现打字效果的文章就介绍到这了,更多相关html css打字效果内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号