修复代码错误,实现线条与图形消失动画的完整攻略!
代码问题分析 在你提供的代码中,`createRandomLine` 函数里的 `line.style.top = `${Math.random() * h}` 存在错误,这里的 `h` 并未定义,应该使用之前定义的 `height` 变量。代码中创建的线条和图形没有实现消失动画的逻辑。 修正后的代码 ```html <!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 { margin: 0; overflow: hidden; background-color: white; } #canvas { display: block; width: 100vw; height: 100vh; } .shape { position: absolute; background-color: black; opacity: 1; transition: opacity 0.3s ease; } .line { position: absolute; background-color: black; transform-origin: 0 0; opacity: 1; transition: opacity 0.3s ease; } </style> </head> <body> <div id="canvas"></div> <script> const canvas = document.getElementById('canvas'); const width = window.innerWidth; const height = window.innerHeight; // 创建随机线条和图形 function createShapes() { const shapesCount = 30 + Math.floor(Math.random() * 20); for (let i = 0; i < shapesCount; i++) { createRandomShape(); } const linesCount = 20 + Math.floor(Math.random() * 15); for (let i = 0; i < linesCount; i++) { createRandomLine(); } // 一段时间后让所有元素消失 setTimeout(() => { const allElements = canvas.querySelectorAll('.shape, .line'); allElements.forEach((element) => { element.style.opacity = 0; // 消失后移除元素 setTimeout(() => { element.remove(); }, 300); }); }, 3000); } function createRandomShape() { const shape = document.createElement('div'); shape.className = 'shape'; // 随机选择形状类型 const shapeType = Math.random(); if (shapeType < 0.3) { // 三角形 shape.style.width = '0'; shape.style.height = '0'; shape.style.borderLeft = `${10 + Math.random() * 50}px solid transparent`; shape.style.borderRight = `${10 + Math.random() * 50}px solid transparent`; shape.style.borderBottom = `${20 + Math.random() * 80}px solid black`; shape.style.backgroundColor = 'transparent'; } else if (shapeType < 0.6) { // 不规则多边形 (使用clip-path) const sides = 3 + Math.floor(Math.random() * 4); // 3-6边形 const points = []; for (let j = 0; j < sides; j++) { const angle = (j / sides) * 2 * Math.PI; const radius = 10 + Math.random() * 40; points.push(`${50 + radius * Math.cos(angle)}% ${50 + radius * Math.sin(angle)}%`); } shape.style.clipPath = `polygon(${points.join(', ')})`; shape.style.width = `${20 + Math.random() * 100}px`; shape.style.height = `${20 + Math.random() * 100}px`; } else { // 矩形或菱形 shape.style.width = `${10 + Math.random() * 120}px`; shape.style.height = `${10 + Math.random() * 120}px`; if (Math.random() > 0.5) { shape.style.transform = 'rotate(45deg)'; } } shape.style.left = `${Math.random() * width}px`; shape.style.top = `${Math.random() * height}px`; shape.style.transform += ` rotate(${Math.random() * 360}deg)`; canvas.appendChild(shape); } function createRandomLine() { const line = document.createElement('div'); line.className = 'line'; const length = 50 + Math.random() * 200; const thickness = 1 + Math.random() * 3; const angle = Math.random() * 360; line.style.width = `${length}px`; line.style.height = `${thickness}px`; line.style.left = `${Math.random() * width}px`; line.style.top = `${Math.random() * height}px`; line.style.transform = `rotate(${angle}deg)`; canvas.appendChild(line); } // 调用创建函数 createShapes(); </script> </body> </html> ``` 额外需要注意的逻辑点 1. 在修改元素样式时,要确保使用正确的变量名,避免出现未定义变量的错误,如这里之前的 `h` 问题。 2. 当使用 `setTimeout` 实现动画效果时,要注意设置合适的时间间隔,避免动画过快或过慢,影响用户体验。 3. 在移除元素时,要确保在动画完成后再进行移除操作,否则可能会导致动画效果不完整。 ######[AI写代码神器 | 1359点数解答 | 2025-06-03 09:38:40]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)365
- 揭秘!十进制整数转其他进制用啥存储结构最合适?答案竟是它!(字节豆包 | 57点数解答 | 2024-11-13 01:21:11)253
- Photoshop 错误 8800 大揭秘:原因分析与实用解决方案(DeepSeek | 349点数解答 | 2025-02-28 11:19:16)607
- MaxScript 导出模型长度到 CSV 文件:代码错误排查与修正全解析 (字节豆包 | 525点数解答 | 2025-07-09 15:37:45)207
- Android开发必备:多种动画API解析及使用注意事项(阿里通义 | 355点数解答 | 2023-11-07 22:46:27)241
- 2 秒动画搞定!“好句子”如何稳停结束位置( | 456点数解答 | 2024-05-19 16:46:15)281
- C++ 实现我的世界基岩版:从简单框架到开发要点揭秘(字节豆包 | 182点数解答 | 2025-02-22 15:53:11)210
- C++ 实现完整斗地主:含洗牌、发牌与手牌展示,可按需扩展!(字节豆包 | 1028点数解答 | 2026-01-10 08:02:37)52
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)336
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 ( | 240点数解答 | 2024-05-20 22:11:29)490
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘( | 47点数解答 | 2024-05-20 22:16:48)376
- 解决 SQL 错误 [1950] [42000]:ORA - 01950 表空间 'users' 无权限问题 ( | 53点数解答 | 2023-12-27 16:21:01)300