酷代码 AI
菜单
全部AI  /  AI写代码神器
服务商

<!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-col

代码问题分析 在你提供的代码中,`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. 在移除元素时,要确保在动画完成后再进行移除操作,否则可能会导致动画效果不完整。 [2025-06-03 09:38:40 | AI写代码神器 | 1359点数解答]

热门AI查看更多
“AI写代码神器”擅长各种语言编程、解释代码、注解补充。 在这里填写你的问题。如:写出java冒泡排序算法。
一个入口掌握所有。 在这输入任何问题,或者要AI输出的内容。
将阿拉伯数字转为人明币大写,如:数字1000转换为壹仟。
AI生成爆文标题,能够快速创作出极具吸引力的标题,促使短视频、自媒体文章的点击率与阅读量迅猛攀升。 此处填写一个原始标题,如:一般人怎样打理自身的社交圈?
AI生成新闻评论。 在这里填入新闻内容。
AI生成内容摘要。 在此处填入需要生成摘要的内容,还可以输入4000字。
AI生成主持词,擅长团建、年会、婚礼、节日等场合的主持词编写。 在这里填入主持场景,如:运动会
AI快速生成周报、月报、日报、年终总结等各类总结报告。 在这里简单输入工作目标、指标、成果,没有任何格式或特殊需求。如:计划年销售业绩1000万,实际完成800万。
输入一个字,显示以这个字开头的歇后语
输入一个字,显示以这个字开头的成语
极速在线生成证件照
极速更换证件照红、蓝、白底色
实用工具查看更多
阿里云99元2核2G服务器/年,199元2核4G服务器随心买。
今日油价 [生活类]
全国各省油价,实时更新。
图片互转base64 [开发类]
将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页、编辑器中。 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方。
时间转换器 [开发类]
时间戳转换器,时间、毫秒、秒、倒计时查看
录入名字、电话、邮箱、个人介绍信息,生成二维码,可通过此码扫码添加微信联系人
数独游戏 [娱乐类]
数独(Sudoku)是经典的9x9数字逻辑谜题。在有81个小格的九宫格内,玩家依据初始数字推理填入1 - 9的数字,要保证每行、每列以及每个3x3宫格中的数字都不重复。这款在线数独游戏有多难度可选,没有头绪时,可以点开答案看一下哦^_^
经典推箱子 [娱乐类]
基于H5的经典推箱子小游戏,锻炼玩家的眼力和反应力,非常不错
AI摸鱼五子棋 [娱乐类]
基于H5的五子棋人机对练,锻炼玩家的眼力和反应力,非常不错
相关提问