<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>爱心跳动</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } .heart-container { position: relative; transform: rotate(45deg);


代码分析 这段代码实现了一个爱心跳动的动画效果,并且在爱心中间显示文字,文字颜色会发生变化,同时还添加了点击爱心加速跳动的交互效果。代码整体结构清晰,不过有一些可以优化的地方。 优化建议 1. 减少代码重复:在 `@keyframes beat` 中,`transform` 属性都包含 `rotate(45deg)`,可以将其提取到 `.heart-container` 的基础样式中,减少重复代码。 2. 提高代码可读性:可以将一些样式值提取为变量,方便修改和维护。 3. 添加浏览器前缀:为了保证动画在不同浏览器中的兼容性,需要添加浏览器前缀。 优化后的代码 ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>爱心跳动</title> <style> /* 定义一些常量 */ :root { --heart-color: #ff4d4d; --text-color-1: #ffffff; --text-color-2: #ffd700; --beat-duration: 1.5s; --beat-duration-fast: 1s; --beat-scale: 1.2; --heart-width: 100px; --heart-height: 150px; } body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } .heart-container { position: relative; transform: rotate(45deg); /* 提取基础旋转样式 */ animation: beat var(--beat-duration) infinite; -webkit-animation: beat var(--beat-duration) infinite; /* 添加浏览器前缀 */ } .heart-container:before, .heart-container:after { content: ""; position: absolute; width: var(--heart-width); height: var(--heart-height); background-color: var(--heart-color); border-radius: 100px 100px 0 0; } .heart-container:before { left: -50px; transform: rotate(-45deg); transform-origin: 100% 100%; } .heart-container:after { top: -50px; transform: rotate(45deg); transform-origin: 0 100%; } .text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: var(--text-color-1); font-family: "Microsoft YaHei", sans-serif; font-size: 24px; font-weight: bold; text-align: center; line-height: 1.2; z-index: 1; animation: colorChange 3s infinite; -webkit-animation: colorChange 3s infinite; /* 添加浏览器前缀 */ } @keyframes beat { 0%, 100% { transform: scale(1); } 10%, 30%, 50%, 70%, 90% { transform: scale(var(--beat-scale)); } } @-webkit-keyframes beat { 0%, 100% { -webkit-transform: scale(1); } 10%, 30%, 50%, 70%, 90% { -webkit-transform: scale(var(--beat-scale)); } } @keyframes colorChange { 0% { color: var(--text-color-1); } 50% { color: var(--text-color-2); } 100% { color: var(--text-color-1); } } @-webkit-keyframes colorChange { 0% { color: var(--text-color-1); } 50% { color: var(--text-color-2); } 100% { color: var(--text-color-1); } } </style> </head> <body> <div class="heart-container"> <div class="text">何铮<br>我们爱你</div> </div> <script> // 可以在这里添加更多交互效果 document.addEventListener('click', function() { const heart = document.querySelector('.heart-container'); heart.style.animationDuration = '1s'; setTimeout(() => { heart.style.animationDuration = '1.5s'; }, 300); }); </script> </body> </html> ``` 额外注意的逻辑点 1. 浏览器兼容性:虽然添加了 `-webkit-` 前缀,但不同浏览器对动画的支持可能还是存在差异,需要在多种浏览器中进行测试。 2. 动画性能:过多的动画可能会影响页面性能,尤其是在移动设备上。可以考虑减少不必要的动画或者使用硬件加速(如 `transform: translateZ(0)`)来提高性能。 3. 用户交互体验:点击爱心加速跳动的效果可以进一步优化,例如添加音效或者其他视觉反馈,提升用户体验。 [2025-06-11 21:06:30 | AI写代码神器 | 1313点数解答]
- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>爱心跳动</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; } .heart-container { position: relative; transform: rotate(45deg); (1313点数解答 | 2025-06-11 21:06:30)102
- <!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title></title> </head> <style> body{display:flex;justify-content:center; margin-top:200px;background-color:#171715;} #container{ display: flex; justify-content: center; align-items: flex-start; height: 500px; } #canvas3d{ width: 500px; height: 500px; } </style> <body> <div id='container'> <canvas i(35点数解答 | 2024-11-09 10:44:18)180
- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>爱心动画</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #000; margin: 0; } .heart { position: relative; width: 100px; height: 90px; animation: beat 1s infinite; } .heart::before, .heart::after { (175点数解答 | 2025-03-08 15:34:12)155
- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>贪吃蛇游戏</title> <style> body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f0f0; } #game-container { position: relative; } #canvas { border: 2px solid #333; background: #fff; } #controls { position: absolute; bottom: 20px; width: 100%; disp(2000点数解答 | 2025-07-10 15:51:52)97
- ```html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>可以成为千一的恋人吗?</title> <style> body { display: flex; justify-content: center; align-items: center; height:100vh; margin: 0; background-color: #ACDEFF; font-family: 'Arial', sans-serif; text-align: center; transition: background-color 0.5s; } .container { background-color: #F9FFFF; padding: 30px; border-radius: 15px; box-shadow: 0 4px8px rgba(0,0,0, 0.1(1271点数解答 | 2025-07-26 08:49:50)102
- <!DOCTYPE html> <html lang="zh - CN"> <head> <meta charset="UTF - 8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>爱心表白</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f5; } .heart { position: relative; width: 100px; height: 90px; (190点数解答 | 2025-02-21 15:37:02)139
- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>在线钢琴</title> <style> body { display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background-color: #f0f0f0; } .piano-container { width: 1000px; height: 600px; background(780点数解答 | 2025-04-19 13:57:24)109
- <!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <title>达尔文头像动画</title> <style> @keyframes wobble { 0% { transform: translate(0, 0) rotate(0deg); } 25% { transform: translate(5px, 0) rotate(3deg); } 50% { transform: translate(0, 0) rotate(0deg); } 75% { transform: translate(-5px, 0) rotate(-3deg); } 100% { transform: translate(0, 0) rotate(0deg); } } .darwin { width: 150px; animation: wobble 2s infinite; } </style> </head> <body> <img src="da(335点数解答 | 2024-12-12 11:33:42)148
- <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>一袋米要扛几楼</title> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="description" content="" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body{margin:0;background:#123;}.base{min-height:100%;display:flex;justify-content:center;align-items:center;background:#123;}.eyeleft{overflow:hidden;width:300px;height:300px;marg(530点数解答 | 2025-07-14 23:05:45)85
- <!DOCTYPE html> <html lang="zh - CN"> <head> <meta charset="UTF - 8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>浪漫之页</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: linear-gradient(to bottom, #FFC0CB, #87CEEB); } .heart { position: relative; width: 100p(152点数解答 | 2025-02-24 21:53:15)138
- <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>幸运大转盘</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; font-family: Arial, sans-serif; background-color: #f0f0f0; } .wheel-container { position: relative; width: 300px; (389点数解答 | 2025-05-01 16:32:00)163
- <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>跳动的心</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #ffe6f2; overflow: hidden; } .heart { position: relative; width: 100p(566点数解答 | 2025-06-06 10:17:14)88