源码地址:GitHub

技术要点


具体实现

index.html HTML结构

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Happy New Year</title>
  7. <link rel="stylesheet" href="style.css">
  8. </head>
  9. <body>
  10. <section>
  11. <h2>Happy New Year<br/><span>2021</span></h2>
  12. </section>
  13. <script src="index.js"></script>
  14. <script>
  15. textShadow("h2", 80);
  16. textShadow("span", 20);
  17. stars();
  18. </script>
  19. </body>
  20. </html>

style.css 样式文件

  1. @import url("https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap");
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. font-family: "Poppins", sans-serif;
  7. }
  8. body {
  9. overflow: hidden;
  10. }
  11. section {
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. min-height: 100vh;
  16. background: linear-gradient(135deg, #111, #222, #111);
  17. }
  18. section::before {
  19. content: "";
  20. position: absolute;
  21. width: 30vw;
  22. height: 30vw;
  23. border: 5vw solid #ff1062;
  24. border-radius: 50%;
  25. box-shadow: 0 0 0 1vw #222,
  26. 0 0 0 1.3vw #fff;
  27. }
  28. section i {
  29. position: absolute;
  30. background: #fff;
  31. border-radius: 50%;
  32. box-shadow: 0 0 10px #fff,
  33. 0 0 20px #fff,
  34. 0 0 40px #fff,
  35. 0 0 80px #fff;
  36. animation: animate 2s linear infinite;
  37. }
  38. @keyframes animate {
  39. 0% {
  40. opacity: 0;
  41. }
  42. 10% {
  43. opacity: 1;
  44. }
  45. 90% {
  46. opacity: 1;
  47. }
  48. 100% {
  49. opacity: 0;
  50. }
  51. }
  52. h2 {
  53. position: absolute;
  54. font-size: 9vw;
  55. color: #fff;
  56. text-align: center;
  57. line-height: 2em;
  58. z-index: 10;
  59. transform: skewY(-7deg);
  60. /* text-shadow: 1px 1px 0 #ccc,
  61. 2px 2px 0 #ccc,
  62. 3px 3px 0 #ccc,
  63. 4px 4px 0 #ccc,
  64. 5px 5px 0 #ccc,
  65. 10px 10px 0 rgba(0, 0, 0, 0.2); */
  66. animation: floating 5s ease-in-out infinite;
  67. }
  68. h2 span {
  69. font-weight: 700;
  70. font-size: 3em;
  71. /* text-shadow: 1px 1px 0 #ccc,
  72. 2px 2px 0 #ccc,
  73. 3px 3px 0 #ccc,
  74. 4px 4px 0 #ccc,
  75. 5px 5px 0 #ccc,
  76. 6px 6px 0 #ccc,
  77. 7px 7px 0 #ccc,
  78. 8px 8px 0 #ccc,
  79. 9px 9px 0 #ccc,
  80. 10px 10px 0 #ccc,
  81. 20px 20px 0 rgba(0, 0, 0, 0.2); */
  82. }
  83. @keyframes floating {
  84. 0%, 100% {
  85. transform: skewY(-7deg) translate(0, -1.5vw);
  86. }
  87. 50% {
  88. transform: skewY(-7deg) translate(0, 1.5vw);
  89. }
  90. }

index.js

  1. /**
  2. * 文字效果
  3. * @param {string} elementQuery 文字元素
  4. * @param {number} cutNumber 切边(text-shadow 高度
  5. */
  6. function textShadow(elementQuery, cutNumber) {
  7. const element = document.querySelector(elementQuery);
  8. let textShadow = "";
  9. let i=0;
  10. for(i; i<Math.floor(element.offsetWidth/cutNumber); i++) {
  11. textShadow += `${i}px ${i}px 0 #ccc,`;
  12. }
  13. element.style.textShadow = textShadow += `${i * 2}px ${i * 2}px 0 rgba(0, 0, 0, 0.2)`;
  14. }
  15. /**
  16. * 星星点状背景实现
  17. */
  18. function stars() {
  19. const count = 200;
  20. const section = document.querySelector("section");
  21. let i = 0;
  22. while(i < count) {
  23. const star = document.createElement("i");
  24. const x = Math.floor(Math.random() * window.innerWidth);
  25. const y = Math.floor(Math.random() * window.innerHeight);
  26. const size = Math.random() * 4;
  27. star.style.left = x + "px";
  28. star.style.top = y + "px";
  29. star.style.width = 1 + size + "px";
  30. star.style.height = 1 + size + "px";
  31. const duration = Math.random() * 2;
  32. star.style.animationDelay = 2 + duration + "s";
  33. section.appendChild(star);
  34. i++;
  35. }
  36. }

内容来源于公众号 @前端先锋