# 彼岸花HTML代码及运行方法 ## HTML代码 ```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 { margin: 0; overflow: hidden; background-color: #000; display: flex; justify-content: center; align-items: center; height: 100vh; } .flower { position: relative; width: 300px; height: 300px; } .petal { position: absolute; width: 60px; height: 120px; background: linear-gradient(to bottom, #ff3366, #cc0066); border-radius: 60px 60px 0 0; transform-origin: bottom center; opacity: 0.8; box-shadow: 0 0 15px #ff0066; } .stamen { position: absolute; width: 20px; height: 60px; background: linear-gradient(to bottom, #ffff00, #ffcc00); border-radius: 10px; top: 50%; left: 50%; transform: translate(-50%, -50%); box-shadow: 0 0 10px #ffff00; } .stem { position: absolute; width: 8px; height: 200px; background: linear-gradient(to bottom, #009900, #006600); top: 100%; left: 50%; transform: translateX(-50%); } </style> </head> <body> <div class="flower" id="flower"></div> <script> const flower = document.getElementById('flower'); const petalCount = 6; // 创建花蕊 const stamen = document.createElement('div'); stamen.className = 'stamen'; flower.appendChild(stamen); // 创建花瓣 for (let i = 0; i < petalCount; i++) { const petal = document.createElement('div'); petal.className = 'petal'; petal.style.transform = `rotate(${i * (360 / petalCount)}deg)`; flower.appendChild(petal); } // 创建花茎 const stem = document.createElement('div'); stem.className = 'stem'; flower.appendChild(stem); // 添加动画效果 let angle = 0; function animate() { angle += 0.5; const petals = document.querySelectorAll('.petal'); petals.forEach((petal, index) => { const rotation = angle + index * (360 / petalCount); petal.style.transform = `rotate(${rotation}deg)`; petal.style.opacity = 0.7 + 0.3 * Math.sin(Date.now() / 500 + index); }); requestAnimationFrame(animate); } animate(); </script> </body> </html> ``` ## 运行方法 1. 将上述代码复制到一个文本编辑器中 2. 将文件保存为`彼岸花.html`(确保文件扩展名是.html) 3. 双击该HTML文件,它将在默认浏览器中打开 4. 你将看到一个红色的彼岸花动画,花瓣会缓慢旋转并轻微闪烁 ## 自定义修改建议 - 修改`petalCount`变量可以改变花瓣数量 - 修改CSS中的颜色值可以改变花朵颜色 - 调整`animate()`函数中的数值可以改变动画速度 ## 三条额外需要注意的逻辑点 1. transform-origin属性确保花瓣围绕底部中心旋转 2. 使用requestAnimationFrame实现平滑动画而非setInterval 3. 花瓣透明度变化使用正弦函数创造自然波动效果 [2025-06-10 22:40:37 | AI写代码神器 | 871点数解答]