ZBLOG

爱心由多个烟花组成

要创建一个由多个烟花组成的跳动爱心效果,你可以使用 HTML、CSS 和 JavaScript。下面是一个简单示例,展示如何实现这个效果。

示例代码

  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>烟花组成的爱心</title>
  7. <style>
  8. body {
  9. display: flex;
  10. justify-content: center;
  11. align-items: center;
  12. height: 100vh;
  13. margin: 0;
  14. background-color: black;
  15. }
  16. .heart {
  17. position: relative;
  18. width: 100px;
  19. height: 100px;
  20. transform: rotate(-45deg);
  21. animation: heartbeat 1s infinite alternate;
  22. }
  23. .heart::before,
  24. .heart::after {
  25. content: '';
  26. position: absolute;
  27. width: 100px;
  28. height: 100px;
  29. background-color: red; /* 可以根据需要更改颜色 */
  30. border-radius: 50%;
  31. }
  32. .heart::before {
  33. top: -50px;
  34. left: 0;
  35. }
  36. .heart::after {
  37. left: 50px;
  38. }
  39. @keyframes heartbeat {
  40. from { transform: scale(1); }
  41. to { transform: scale(1.2); }
  42. }
  43. /* 烟花样式 */
  44. .firework {
  45. position:absolute;
  46. bottom:-150%;
  47. opacity:.9;
  48. border-radius:.5rem;
  49. box-shadow:
  50. inset -5px -5px 10px rgba(255,255,255,.7),
  51. inset -20px -20px 15px rgba(255,165,0,.5),
  52. inset -25px -25px 25px rgba(255,20,147,.4);
  53. animation:
  54. explode ease-in-out forwards,
  55. fadeOut ease forwards;
  56. }
  57. @keyframes explode{
  58. from{
  59. transform :translateY(70%) scale(1);
  60. }
  61. to{
  62. transform : translateY(-300%) scale(.5)
  63. }
  64. }
  65. @keyframes fadeOut{
  66. from{
  67. opacity :1
  68. }
  69. to{
  70. opacity :0
  71. }
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div class="heart"></div>
  77. <script>
  78. function createFirework() {
  79. const firework = document.createElement('div');
  80. firework.className = 'firework';
  81. // 随机设置烟花的位置和大小
  82. const size = Math.random() * (30 - 10) + 10; // 随机大小在10到30之间
  83. firework.style.width = `${size}px`;
  84. firework.style.height = `${size}px`;
  85. // 设置随机位置
  86. firework.style.left = `${Math.random() * window.innerWidth}px`;
  87. document.body.appendChild(firework);
  88. // 在3秒后移除烟花元素
  89. setTimeout(() => {
  90. firework.remove();
  91. }, 2000);
  92. }
  93. // 每隔500毫秒生成一次烟花
  94. setInterval(createFirework, 500);
  95. </script>
  96. </body>
  97. </html>

如何使用

  1. 将上述代码复制并粘贴到一个新的文本文件中。
  2. 将该文件保存为 index.html
  3. 使用浏览器打开此文件。

效果说明

  • 页面中央会出现一个由红色背景的爱心图形,并且有“跳动”的动画效果。
  • 不断生成不同颜色和大小的“烟花”效果,从底部向上发射。

你可以根据需要调整烟花的生成频率、颜色和其他特效,以便达到自己想要的视觉效果。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=20474

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?