ul左边padding-inline-start: 40px;多出宽度和高度

博客指出浏览器默认会有边距和填充,除IE浏览器外,ul会自动向右浮动40px。提供了解决方法,一是在当前页面css用*选择器重置所有标签,二是在全局样式里重置,还可定义ul{ overflow:hidden}来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个是浏览器的原因默认会有边距和填充。

好像默认的除IE浏览器,ul都会自动向右浮动40px

清除掉写下面的代码就正常了

方法1.在当前页面css使用*选择器 选择所有标签重置
*{ margin:0; padding:0; }

方法2.推荐:在全局样式里重置

定义一个ul{ overflow:hidden}解决问题

body, div, img,dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, table, th, td {margin:0; padding:0;}  

引用①来自:这位朋友
引用②来自:这位朋友

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CAD属性块分类树状视图</title> <style> /* 禁用整个页面的滚动条 */ html, body { margin: 0; padding: 0; height: 100%; width: 100%; overflow: hidden; font-family: "Helvetica Neue", Helvetica, Arial, "PingFang SC", "Hiragino Sans GB", "Heiti SC", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif; white-space: nowrap; } /* 主容器 - 固定宽度避免闪烁 */ .container { display: flex; flex-direction: column; height: 100vh; width: 100vw; max-width: 100vw; overflow: hidden; background-color: rgba(255, 255, 255, 0.9); position: relative; } /* 标题样式 */ .header { background: linear-gradient(to right, #2c3e50, #4a6491); color: white; padding: 15px 20px; text-align: center; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } .header h1 { margin: 0; font-size: 24px; font-weight: 600; } /* 内容区域 */ .content { display: flex; flex: 1; overflow: hidden; flex-direction: column; flex-wrap: nowrap; justify-content: flex-start; } .icon { color: #f1c40f; padding-right: 3px; } /* 树状结构容器 */ .tree-container { flex: 1; background: rgb(59 68 83); color: white; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08); padding: 0px 5px 5px 5px; display: flex; flex-direction: column; overflow: hidden; } /* 树状结构样式 */ .tree { list-style: none; padding: 0; margin: 0; } .tree, .tree ul { list-style: none; padding-left: 25px; } .tree li { margin: 2px 0; position: relative; } .node { display: flex; cursor: pointer; transition: all 0.2s; background-color: rgb(0 0 0 / 35%); flex-direction: row; flex-wrap: nowrap; justify-content: flex-start; white-space: nowrap; font-size: 1rem; } .node:hover { background-color: #877739; } .toggle { width: 20px; height: 20px; margin-right: 10px; display: flex; align-items: center; justify-content: center; background-color: #4a6572; color: white; font-size: 14px; cursor: pointer; transition: all 0.2s; } .toggle:hover { background-color: #344955; } .node-name { flex: 1; color: white; } .node-count { padding: 2px 8px; border-radius: 10px; font-size: 12px; min-width: 30px; text-align: center; } /* 自定义滚动条 */ .tree-scroll { flex: 1; overflow-y: scroll; overflow-x: auto; padding-right: 5px; border: 1px solid rgb(0 0 0); box-shadow: 0 1px 1px rgb(59 63 89); } /* 自定义滚动条样式 */ .tree-scroll::-webkit-scrollbar { width: 0px; } /* 整个水平滚动条容器 */ .tree-scroll::-webkit-scrollbar:horizontal { height: 10px; } .tree-scroll::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 1px; } /* 水平滚动条轨道 */ .tree-scroll::-webkit-scrollbar-track:horizontal { background: #f1f1f1; /* 轨道背景色 */ border-radius: 5px; /* 圆角 */ } .tree-scroll::-webkit-scrollbar-thumb { background: #a0a0a0; border-radius: 1px; } /* 水平滚动条滑块 */ .tree-scroll::-webkit-scrollbar-thumb:horizontal { border-radius: 5px; /* 滑块圆角 */ border: 1px solid #f1f1f1; /* 滑块框 */ } .tree-scroll::-webkit-scrollbar-thumb:hover { background: #808080; } /* 鼠标悬停时滑块样式 */ .tree-scroll::-webkit-scrollbar-thumb:horizontal:hover { background: #808080; } /* 统计信息面板 */ .stats-panel header { font-size: 14px; color: white; border-bottom: 1px dashed rgb(0 0 0); } .stats-panel { width: auto; margin-left: 0; margin-top: 5px; border: 1px solid rgb(0 0 0); } .stat-item { font-size: 12px; display: flex; justify-content: space-between; border-bottom: 1px dashed rgb(0 0 0); flex-direction: row; flex-wrap: nowrap; text-align: center; } .stat-item:last-child { border-bottom: none; } .stat-label { flex: 1; color: #f1c40f; display: flex; flex-direction: row; flex-wrap: nowrap; text-align: center; justify-content: flex-start; align-items: center; white-space: nowrap; padding-left: 2px; border-right: 1px dashed rgb(0 0 0); } .stat-value { flex: 1; color: #4ec3af; display: flex; white-space: nowrap; flex-direction: row; flex-wrap: nowrap; text-align: center; justify-content: flex-end; align-items: center; padding-right: 2px; } .search-box { width: 10%; position: relative; margin-top: 2px; border-bottom: 1px solid rgb(0 0 0); } .search-box input { padding: 0px 15px 0px 5px; border-radius: 30px; font-size: 14px; outline: none; transition: all 0.3s; } .search-box input:focus { } .search-icon { position: absolute; left: 15px; top: 50%; transform: translateY(-50%); color: #7f8c8d; } /* 响应式设计 */ @media (max-width: 768px) { .content { flex-direction: column; } } </style> </head> <body> <div class="container"> <div class="content"> <div class="tree-container"> <div class="tree-scroll" id="treeScroll"> <ul class="tree" id="treeRoot"> <!-- 树状结构将通过JavaScript动态生成 --> </ul> </div> <div class="stats-panel"> <header>属性</header> <div class="stat-item"> <span class="stat-label">名称:</span> <span class="stat-value" id="att_tag1">气动隔膜阀</span> </div> <div class="stat-item"> <span class="stat-label">位号:</span> <span class="stat-value" id="att_tag2" color>B1TA4001AV101</span> </div> <div class="stat-item"> <span class="stat-label">设备描述:</span> <span class="stat-value" id="att_tag3"></span> </div> <div class="stat-item"> <span class="stat-label">材质:</span> <span class="stat-value" id="att_tag4">文本标注 (5)</span> </div> <div class="stat-item"> <span class="stat-label">介质名称(中):</span> <span class="stat-value" id="att_tag5">17.75</span> </div> <div class="stat-item"> <span class="stat-label">介质粘度:</span> <span class="stat-value" id="att_tag6">17.75</span> </div> <div class="stat-item"> <span class="stat-label">介质密度:</span> <span class="stat-value" id="att_tag7">17.75</span> </div> <div class="stat-item"> <span class="stat-label">DOCUMENTNO_1:</span> <span class="stat-value" id="att_tag8">17.75</span> </div> <div class="stat-item"> <span class="stat-label">连接类型:</span> <span class="stat-value" id="att_tag7">17.75</span> </div> </div> </div> </div> </div> <script> // CAD属性块数据 const cadData = { name: "所有属性块", count: 142, children: [ { name: "机械部件", count: 45, children: [ { name: "齿轮", count: 12 }, { name: "轴承", count: 18 }, { name: "联轴器", count: 8 }, { name: "紧固件", count: 7 } ] }, { name: "电气元件", count: 32, children: [ { name: "开关", count: 10 }, { name: "继电器", count: 8 }, { name: "变压器", count: 6 }, { name: "连接器", count: 8 } ] }, { name: "管道系统", count: 28, children: [ { name: "阀门", count: 9 }, { name: "法兰", count: 7 }, { name: "管接头", count: 8 }, { name: "过滤器", count: 4 } ] }, { name: "结构组件", count: 22, children: [ { name: "梁", count: 8 }, { name: "柱", count: 7 }, { name: "支架", count: 5 }, { name: "底座", count: 2 } ] }, { name: "注释符号", count: 15, children: [ { name: "尺寸标注", count: 6 }, { name: "表面粗糙度", count: 4 }, { name: "焊接符号", count: 3 }, { name: "形位公差", count: 2 } ] } ] }; // 搜索功能 function setupSearch() { const searchInput = document.getElementById('searchInput'); searchInput.addEventListener('input', function() { const searchTerm = this.value.toLowerCase().trim(); const allLeafNodes = document.querySelectorAll('.tree-scroll'); // 重置所有样式 allLeafNodes.forEach(node => { node.classList.remove('highlight'); node.style.display = ''; }); // 如果没有搜索词,显示所有节点 if (searchTerm === '') { // 展开所有节点 document.querySelectorAll('node').forEach(detail => { detail.open = false; }); return; } // 搜索匹配项并高亮显示 let found = false; allLeafNodes.forEach(node => { const itemName = node.querySelector('.item-name').textContent.toLowerCase(); if (itemName.includes(searchTerm)) { node.classList.add('highlight'); found = true; // 展开父节点 let parent = node.closest('details'); while (parent) { parent.open = true; parent = parent.parentElement.closest('details'); } } else { node.style.display = 'none'; } }); if (!found) { // 如果没有找到匹配项,显示所有节点 allLeafNodes.forEach(node => { node.style.display = ''; }); } }); } // 生成树状结构 function generateTree(data, parentElement) { const li = document.createElement('li'); const node = document.createElement('div'); node.className = 'node'; const toggle = document.createElement('div'); toggle.className = 'toggle'; toggle.textContent = '➕'; const icon = document.createElement('div'); icon.className = 'icon'; icon.textContent = '👮'; const nameSpan = document.createElement('span'); nameSpan.className = 'node-name'; nameSpan.textContent = data.name; const countSpan = document.createElement('span'); countSpan.className = 'node-count'; countSpan.textContent = data.count; node.appendChild(icon); node.appendChild(nameSpan); node.appendChild(countSpan); node.appendChild(toggle); li.appendChild(node); if (data.children && data.children.length > 0) { const childUl = document.createElement('ul'); data.children.forEach(child => { generateTree(child, childUl); }); li.appendChild(childUl); // 添加展开/折叠事件 toggle.addEventListener('click', function(e) { e.stopPropagation(); const isExpanded = childUl.style.display !== 'none'; if (isExpanded) { childUl.style.display = 'none'; toggle.textContent = '➕'; node.classList.remove('expanded'); } else { childUl.style.display = 'block'; toggle.textContent = '➖'; node.classList.add('expanded'); } }); // 初始折叠状态 childUl.style.display = 'none'; } else { toggle.style.visibility = 'hidden'; } parentElement.appendChild(li); } // 初始化树状视图 document.addEventListener('DOMContentLoaded', function() { const treeRoot = document.getElementById('treeRoot'); generateTree(cadData, treeRoot); }); </script> </body> </html> 帮我修改这个代码,要求在树容器顶部加一个搜索框,只要根据输入内容在树节点中搜到结果,就会展开对应节点且高亮显示搜索结果
07-03
<!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> .gallery-container { display: flex; flex-wrap: wrap; gap: 30px; max-width: 90%; margin: 0 auto; padding: 20px; background: #fff; /* 移除外框阴影 */ border-radius: 10px; } .gallery-section { flex: 1; min-width: 40%; } .description-section { flex: 1; min-width: 40%; padding: 20px; background: #fff; border-radius: 8px; /* 减小整体字体大小 */ font-size: 14px; } /* 添加标题样式 */ .section-title { font-size: 2.0em; /* 比正文稍大但比原设计小 */ font-weight: bold; color: #0566a8; margin-top: 0; padding-bottom: 0px; /*border-bottom: 1px solid #eee;*/ margin-bottom: 0px; } /* 添加联系按钮样式 */ .contact-button { display: inline-block; margin-top: 20px; padding: 10px 20px; background-color: #0566a8; color: white; text-decoration: none; border-radius: 4px; font-weight: 500; transition: all 0.3s ease; border: none; cursor: pointer; font-size: 16px; text-align: center; width: 100%; box-sizing: border-box; } .contact-button:hover { background-color: #034a7a; transform: translateY(-2px); box-shadow: 0 4px 8px rgba(0,0,0,0.1); } .main-image { width: 100%; border-radius: 8px; box-shadow: 0 5px 15px rgba(0,0,0,0.2); cursor: pointer; transition: transform 0.3s; } .main-image:hover { transform: scale(1.01); } .thumbnails { display: flex; justify-content: center; gap: 10px; margin-top: 15px; overflow-x: auto; padding: 10px 0; } .thumbnail { width: 70px; height: 70px; border-radius: 5px; cursor: pointer; object-fit: cover; border: 2px solid transparent; transition: all 0.3s; opacity: 0.7; } .thumbnail:hover, .thumbnail.active { opacity: 1; border-color: #0566a8; transform: scale(1.05); } /* 居中模态框样式 */ .modal { display: none; position: fixed; z-index: 1000; top: 0; left: 10%; width: 100%; height: 100%; background-color: rgba(0,0,0,0.9); overflow: auto; } .modal-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); max-width: 80%; max-height: 80%; object-fit: contain; border-radius: 5px; box-shadow: 0 0 30px rgba(0,0,0,0.6); } /* 导航按钮样式 */ .nav-btn { position: fixed; top: 50%; transform: translateY(-50%); color: white; font-size: 40px; cursor: pointer; background: rgba(0,0,0,0.3); width: 60px; height: 60px; display: flex; justify-content: center; align-items: center; border-radius: 50%; z-index: 1002; transition: all 0.3s; } .nav-btn:hover { background: rgba(0,0,0,0.6); transform: translateY(-50%) scale(1.1); } .prev-btn { left: 12%; } .next-btn { right: 12%; } .close { position: fixed; top: 5%; right: 12%; color: #f1f1f1; font-size: 40px; font-weight: bold; cursor: pointer; transition: 0.3s; z-index: 1001; background: rgba(0,0,0,0.3); width: 50px; height: 50px; display: flex; justify-content: center; align-items: center; border-radius: 50%; } .close:hover { color: #bbb; background: rgba(0,0,0,0.6); transform: scale(1.1); } .feature-list { margin: 0; padding-left: 20px; /* 减小列表字体大小 */ font-size: 0.95em; } .feature-list li { margin-bottom: 10px; position: relative; } .feature-list li::before { content: "&bull;"; color: #3498db; position: absolute; left: -15px; font-size: 1.2em; } @media (max-width: 768px) { .gallery-container { flex-direction: column; max-width: 95%; } .thumbnails { justify-content: flex-start; } .nav-btn { width: 40px; height: 40px; font-size: 24px; } .close { font-size: 30px; width: 40px; height: 40px; } .section-title { font-size: 1.2em; } } </style> </head> <body> <div class="gallery-container"> <!-- 左侧图片画廊 --> <div class="gallery-section"> <img id="mainImage" class="main-image" src="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月9日-11_40_06_compressed.png" alt="图集"> <div class="thumbnails"> <img class="thumbnail active" src="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月9日-11_40_06_compressed.png" alt="图一" data-large="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月9日-11_40_06_compressed.png"> <img class="thumbnail" src="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月10日-11_05_58_compressed.png" alt="图二" data-large="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月10日-11_05_58_compressed.png"> <img class="thumbnail" src="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月10日-11_38_09_compressed.png" alt="图三" data-large="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月10日-11_38_09_compressed.png"> <img class="thumbnail" src="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月10日-13_55_14_compressed.png" alt="图四" data-large="https://www.fetchingpack.com/wp-content/uploads/2025/07/ChatGPT-Image-2025年6月10日-13_55_14_compressed.png"> </div> </div> <!-- 右侧文字描述 - 添加了标题 --> <div class="description-section"> <!-- 添加的标题 --> <h2 class="section-title">collapsible rigid box</h2> <div class="description-content"> <ul class="feature-list"> <li>We offer collapsible rigid boxes in three sizes—Large, Medium, and Tiny,each a proprietary structure we developed in-house and protected by our patents.<br>Image 1- Large:Suitable for magnetic gift boxes with product height of 100-150mm.<br>Image 2- Medium:Suitable for magnetic gift boxes with product height of 50-100mm.<br>Image 3- Tiny:Suitable for magnetic gift boxes with product height of 20-50mm.</li> <li>Our collapsible rigid box, can save more than 60% of shipping and storage costs compared with rigid gift box,save 20%-30% costs compared with standard collapsible rigid box.</li> <li>Can be apply cold or hot foil stamping, UV, frosted touch, embossing and other surface processes to enhance the overall packaging visual effect.</li> <li>Using automatic visual positioning boxes production line, short production time and stable quality.</li> <li>FSC certified material, environmentally friendly and recyclable.</li> </ul> <!-- 添加的联系按钮 --> <a href="https://www.fetchingpack.com/contact-us/" class="contact-button">Contact Us</a> </div> </div> </div> <!-- 图片模态框 --> <div id="imageModal" class="modal"> <span class="close">×</span> <div class="prev-btn nav-btn">❮</div> <div class="next-btn nav-btn">❯</div> <img class="modal-content" id="expandedImage"> </div> <script> // 获取DOM元素 const mainImage = document.getElementById('mainImage'); const thumbnails = document.querySelectorAll('.thumbnail'); const modal = document.getElementById('imageModal'); const expandedImg = document.getElementById('expandedImage'); const closeBtn = document.querySelector('.close'); const prevBtn = document.querySelector('.prev-btn'); const nextBtn = document.querySelector('.next-btn'); // 当前选中的图片索引 let currentIndex = 0; // 缩略图点击事件 thumbnails.forEach((thumb, index) => { thumb.addEventListener('click', function() { // 更新主图 updateMainImage(this.dataset.large, this.alt); // 更新当前索引 currentIndex = index; // 更新激活状态 thumbnails.forEach(t => t.classList.remove('active')); this.classList.add('active'); }); }); // 更新主图函数 function updateMainImage(src, alt) { mainImage.src = src; mainImage.alt = alt; } // 主图点击事件 - 打开模态框 mainImage.addEventListener('click', function() { openModal(this.src, this.alt); }); // 打开模态框函数 function openModal(src, alt) { modal.style.display = 'block'; expandedImg.src = src; expandedImg.alt = alt; document.body.style.overflow = 'hidden'; // 防止背景滚动 } // 关闭模态框函数 function closeModal() { modal.style.display = 'none'; document.body.style.overflow = ''; // 恢复背景滚动 } // 关闭按钮点击事件 closeBtn.addEventListener('click', closeModal); // 点击模态框背景关闭 modal.addEventListener('click', function(e) { if (e.target === modal) { closeModal(); } }); // ESC键关闭模态框 document.addEventListener('keydown', function(e) { if (e.key === 'Escape' && modal.style.display === 'block') { closeModal(); } }); // 上一张按钮 prevBtn.addEventListener('click', function(e) { e.stopPropagation(); // 防止触发背景关闭 navigate(-1); }); // 下一张按钮 nextBtn.addEventListener('click', function(e) { e.stopPropagation(); // 防止触发背景关闭 navigate(1); }); // 键盘左右键导航 document.addEventListener('keydown', function(e) { if (modal.style.display !== 'block') return; if (e.key === 'ArrowLeft') { navigate(-1); } else if (e.key === 'ArrowRight') { navigate(1); } }); // 导航函数 function navigate(direction) { currentIndex += direction; // 循环处理 if (currentIndex < 0) { currentIndex = thumbnails.length - 1; } else if (currentIndex >= thumbnails.length) { currentIndex = 0; } // 获取新的缩略图 const newThumb = thumbnails[currentIndex]; // 更新模态框中的图片 expandedImg.src = newThumb.dataset.large; expandedImg.alt = newThumb.alt; // 更新激活状态 thumbnails.forEach(t => t.classList.remove('active')); newThumb.classList.add('active'); // 更新主图 updateMainImage(newThumb.dataset.large, newThumb.alt); } // 添加触摸滑动支持 let touchStartX = 0; expandedImg.addEventListener('touchstart', (e) => { touchStartX = e.touches[0].clientX; }); expandedImg.addEventListener('touchend', (e) => { const touchEndX = e.changedTouches[0].clientX; const diffX = touchStartX - touchEndX; if (Math.abs(diffX) > 50) { // 滑动离阈值 if (diffX > 0) { navigate(1); // 向左滑动,下一张 } else { navigate(-1); // 向右滑动,上一张 } } }); </script> </body> </html> 如何缩短标题文本之间的
最新发布
07-24
保持整体代码加入结束胜利动画,自动翻开避开正确选项增加错误反馈<!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> * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } body { background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%); min-height: 100vh; padding: 20px; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #333; } .container { width: 95%; max-width: 1200px; background: rgba(255, 255, 255, 0.95); border-radius: 20px; padding: 30px; box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2); position: relative; overflow: hidden; } header { text-align: center; margin-bottom: 25px; position: relative; z-index: 1; } h1 { color: #2c3e50; font-size: 2.8rem; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1); margin-bottom: 10px; letter-spacing: 1px; background: linear-gradient(90deg, #ff6b6b, #4ecdc4); -webkit-background-clip: text; background-clip: text; color: transparent; } .subtitle { color: #7f8c8d; font-size: 1.2rem; margin-bottom: 25px; } .controls { display: flex; flex-wrap: wrap; gap: 20px; margin-bottom: 30px; background: rgba(236, 240, 241, 0.7); padding: 20px; border-radius: 15px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05); z-index: 1; position: relative; } .control-group { display: flex; flex-direction: column; flex: 1; min-width: 200px; } .control-group label { margin-bottom: 8px; color: #2c3e50; font-weight: bold; font-size: 1.1rem; } .slider-container { display: flex; align-items: center; gap: 15px; } input[type="range"] { flex: 1; -webkit-appearance: none; height: 12px; border-radius: 10px; background: linear-gradient(to right, #ff9a9e, #fad0c4); outline: none; } input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 25px; height: 25px; border-radius: 50%; background: #3498db; cursor: pointer; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); border: 3px solid white; } .count-display { background: #3498db; color: white; padding: 5px 15px; border-radius: 20px; font-weight: bold; min-width: 60px; text-align: center; } button { background: #3498db; color: white; border: none; padding: 12px 25px; border-radius: 50px; cursor: pointer; font-size: 1.1rem; font-weight: bold; transition: all 0.3s ease; box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3); display: flex; align-items: center; justify-content: center; gap: 8px; } button:hover { transform: translateY(-3px); box-shadow: 0 6px 12px rgba(52, 152, 219, 0.4); background: #2980b9; } button:active { transform: translateY(1px); } .auto-flip-controls { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 15px; } .auto-btn { background: #9b59b6; padding: 10px 15px; font-size: 0.9rem; } .auto-btn.active { background: #8e44ad; box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.2); } .timer { font-size: 1.8rem; font-weight: bold; color: #e74c3c; text-align: center; margin: 20px 0; background: rgba(255, 255, 255, 0.8); padding: 15px; border-radius: 15px; display: inline-block; min-width: 200px; } .game-board { display: grid; grid-template-columns: repeat(6, 1fr); gap: 15px; margin: 0 auto; perspective: 1000px; max-width: 800px; } .card { aspect-ratio: 1/1; background: white; border-radius: 15px; display: flex; align-items: center; justify-content: center; cursor: pointer; position: relative; transform-style: preserve-3d; transition: transform 0.5s, box-shadow 0.3s; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); font-size: 1.4rem; font-weight: bold; color: #4a4a4a; text-align: center; padding: 10px; } .card:hover { box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15); } .card.flipped { transform: rotateY(180deg); } .card.matched { animation: matchedAnimation 0.8s forwards; } .card .front, .card .back { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; align-items: center; justify-content: center; border-radius: 15px; padding: 15px; } .card .front { background: linear-gradient(135deg, #3498db, #2ecc71); color: white; transform: rotateY(180deg); font-size: 1.3rem; } .card .back { background: white; border: 3px solid #3498db; font-size: 1.8rem; } @keyframes matchedAnimation { 0% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.2); opacity: 0.7; } 100% { transform: scale(0); opacity: 0; visibility: hidden; } } .instructions { background: rgba(236, 240, 241, 0.7); padding: 20px; border-radius: 15px; margin-top: 30px; } .instructions h3 { color: #2c3e50; margin-bottom: 15px; text-align: center; } .instructions ul { padding-left: 20px; } .instructions li { margin-bottom: 10px; line-height: 1.5; } footer { text-align: center; margin-top: 30px; color: #ecf0f1; font-size: 0.9rem; opacity: 0.9; } @media (max-width: 768px) { .game-board { grid-template-columns: repeat(4, 1fr); } h1 { font-size: 2.2rem; } } @media (max-width: 480px) { .game-board { grid-template-columns: repeat(3, 1fr); } .container { padding: 15px; } h1 { font-size: 1.8rem; } .auto-flip-controls { flex-direction: column; } .auto-btn { width: 100%; } } </style> </head> <body> <div class="container"> <header> <h1>单词消消乐</h1> <p class="subtitle">自动翻开功能演示 - 提升学习体验</p> </header> <div class="controls"> <div class="control-group"> <label for="word-count">单词数量:</label> <div class="slider-container"> <input type="range" id="word-count" min="3" max="12" step="1" value="6"> <span class="count-display" id="word-count-display">6</span> </div> </div> <div class="control-group"> <label>自动翻开控制:</label> <div class="auto-flip-controls"> <button id="sequential-btn" class="auto-btn">顺序翻开</button> <button id="random-btn" class="auto-btn">随机翻开</button> <button id="smart-btn" class="auto-btn">智能匹配</button> <button id="stop-btn" class="auto-btn">停止自动翻开</button> </div> </div> <div class="control-group"> <label>游戏控制:</label> <div style="display: flex; gap: 10px;"> <button id="start-btn">🎮 开始游戏</button> <button id="reset-btn">🔄 重新开始</button> </div> </div> </div> <div class="timer" id="timer">耗时:00:00</div> <div class="game-board" id="game-board"></div> <div class="instructions"> <h3>自动翻开功能说明</h3> <ul> <li><strong>顺序翻开</strong>:按照卡片位置顺序依次翻开单词</li> <li><strong>随机翻开</strong>:随机选择卡片翻开,展示不同单词</li> <li><strong>智能匹配</strong>:自动寻找匹配的单词对并翻开</li> <li><strong>停止自动翻开</strong>:停止所有自动翻开操作,恢复手动模式</li> </ul> </div> <footer> <p>单词消消乐游戏 - 自动翻开功能实现 | 提升学习效率</p> </footer> </div> <script> // 游戏状态变量 let gameState = { words: [], cards: [], flippedCards: [], matchedPairs: 0, totalPairs: 0, gameStarted: false, timer: null, seconds: 0, wordCount: 6, autoFlipInterval: null, autoFlipMode: null }; // 默认词库 const defaultWords = [ ["Monday", "星期一"], ["Tuesday", "星期二"], ["Wednesday", "星期三"], ["Thursday", "星期四"], ["Friday", "星期五"], ["Saturday", "星期六"], ["Sunday", "星期天"], ["weekend", "周末"], ["wash", "洗"], ["watch", "看"], ["do", "做,干"], ["read", "看,读"], ["play", "踢,玩,参加"], ["cooking", "烹饪"], ["often", "经常"], ["park", "公园"], ["tired", "疲倦的"], ["sport", "运动"], ["should", "应该,应当"], ["every", "每一个"], ["day", "日子,天"], ["schedule", "日程安排"] ]; // 初始化游戏 function initGame() { // 事件监听 document.getElementById('word-count').addEventListener('input', updateWordCountDisplay); document.getElementById('start-btn').addEventListener('click', startGame); document.getElementById('reset-btn').addEventListener('click', resetGame); // 自动翻开按钮事件 document.getElementById('sequential-btn').addEventListener('click', () => startAutoFlip('sequential')); document.getElementById('random-btn').addEventListener('click', () => startAutoFlip('random')); document.getElementById('smart-btn').addEventListener('click', () => startAutoFlip('smart')); document.getElementById('stop-btn').addEventListener('click', stopAutoFlip); updateWordCountDisplay(); // 使用默认词表 gameState.words = [...defaultWords]; } // 更新单词数量显示 function updateWordCountDisplay() { const slider = document.getElementById('word-count'); const display = document.getElementById('word-count-display'); gameState.wordCount = parseInt(slider.value); display.textContent = gameState.wordCount; } // 开始游戏 function startGame() { if (gameState.words.length === 0) { alert('请先导入词表或使用默认词表!'); return; } // 重置游戏状态 resetGame(); generateCards(); } // 重置游戏 function resetGame() { // 清除计时器自动翻开 clearInterval(gameState.timer); clearInterval(gameState.autoFlipInterval); gameState.timer = null; gameState.autoFlipInterval = null; gameState.seconds = 0; document.getElementById('timer').textContent = '耗时:00:00'; // 重置游戏状态 gameState.flippedCards = []; gameState.matchedPairs = 0; gameState.gameStarted = true; // 生成新卡片 generateCards(); } // 生成卡片 function generateCards() { gameState.cards = []; gameState.matchedPairs = 0; gameState.totalPairs = Math.min(gameState.wordCount, gameState.words.length); // 获取单词对 const selectedWords = []; for (let i = 0; i < gameState.totalPairs; i++) { if (i < gameState.words.length) { selectedWords.push(gameState.words[i]); } } // 创建卡片对 selectedWords.forEach(pair => { gameState.cards.push({ type: 'word', content: pair[0], pairId: pair[0] + pair[1], matched: false }); gameState.cards.push({ type: 'answer', content: pair[1], pairId: pair[0] + pair[1], matched: false }); }); // 洗牌 shuffleCards(gameState.cards); // 渲染游戏面板 renderGameBoard(); // 开始计时 startTimer(); } // 洗牌算法 function shuffleCards(cards) { for (let i = cards.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [cards[i], cards[j]] = [cards[j], cards[i]]; } } // 渲染游戏面板 function renderGameBoard() { const gameBoard = document.getElementById('game-board'); gameBoard.innerHTML = ''; gameState.cards.forEach((card, index) => { const cardElement = document.createElement('div'); cardElement.className = 'card'; cardElement.dataset.index = index; // 添加卡片内容 const back = document.createElement('div'); back.className = 'back'; back.textContent = '?'; const front = document.createElement('div'); front.className = 'front'; front.textContent = card.content; cardElement.appendChild(back); cardElement.appendChild(front); // 添加点击事件 cardElement.addEventListener('click', () => flipCard(cardElement, index)); // 如果已匹配,隐藏卡片 if (card.matched) { cardElement.classList.add('matched'); } // 如果已翻开但未匹配,显示正面 else if (gameState.flippedCards.includes(index)) { cardElement.classList.add('flipped'); } // 设置随机背景颜色 const colors = ['#ff9ff3', '#1dd1a1', '#5f27cd', '#ff9f43', '#a55c1b', '#2e86de']; const colorIndex = Math.floor(Math.random() * colors.length); front.style.background = colors[colorIndex]; gameBoard.appendChild(cardElement); }); } // 翻开卡片 function flipCard(cardElement, index) { // 游戏未开始或已匹配的卡片不能翻开 if (!gameState.gameStarted || gameState.cards[index].matched || gameState.flippedCards.includes(index) || gameState.flippedCards.length >= 2) { return; } // 翻开卡片 cardElement.classList.add('flipped'); gameState.flippedCards.push(index); // 检查是否匹配 if (gameState.flippedCards.length === 2) { setTimeout(checkMatch, 500); } } // 检查匹配 function checkMatch() { const [index1, index2] = gameState.flippedCards; const card1 = gameState.cards[index1]; const card2 = gameState.cards[index2]; if (card1.pairId === card2.pairId) { // 匹配成功 card1.matched = true; card2.matched = true; gameState.matchedPairs++; document.querySelector(`.card[data-index="${index1}"]`).classList.add('matched'); document.querySelector(`.card[data-index="${index2}"]`).classList.add('matched'); // 检查游戏是否结束 if (gameState.matchedPairs === gameState.totalPairs) { endGame(); } } // 清空已翻开卡片 gameState.flippedCards = []; } // 开始计时 function startTimer() { if (gameState.timer) return; gameState.seconds = 0; updateTimerDisplay(); gameState.timer = setInterval(() => { gameState.seconds++; updateTimerDisplay(); }, 1000); } // 更新计时器显示 function updateTimerDisplay() { const minutes = Math.floor(gameState.seconds / 60); const seconds = gameState.seconds % 60; document.getElementById('timer').textContent = `耗时:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; } // 开始自动翻开 function startAutoFlip(mode) { // 停止之前的自动翻开 stopAutoFlip(); gameState.autoFlipMode = mode; // 根据模式设置不同的时间间隔 const interval = mode === 'smart' ? 1500 : 1000; // 获取所有未匹配的卡片索引 const unmatchedCards = []; gameState.cards.forEach((card, index) => { if (!card.matched && !gameState.flippedCards.includes(index)) { unmatchedCards.push(index); } }); if (unmatchedCards.length === 0) return; // 开始自动翻开 gameState.autoFlipInterval = setInterval(() => { if (unmatchedCards.length === 0) { stopAutoFlip(); return; } let cardIndex; // 根据模式选择卡片 if (mode === 'sequential') { cardIndex = unmatchedCards.shift(); } else if (mode === 'random') { const randomIndex = Math.floor(Math.random() * unmatchedCards.length); cardIndex = unmatchedCards.splice(randomIndex, 1)[0]; } else if (mode === 'smart') { // 智能模式:尝试找到匹配对 const result = findSmartPair(unmatchedCards); if (result) { // 翻开匹配对 const [card1, card2] = result; flipCardAutomatically(card1); setTimeout(() => flipCardAutomatically(card2), 500); // 从数组中移除 const index1 = unmatchedCards.indexOf(card1); if (index1 > -1) unmatchedCards.splice(index1, 1); const index2 = unmatchedCards.indexOf(card2); if (index2 > -1) unmatchedCards.splice(index2, 1); return; } else { // 没有找到匹配对,随机翻开一个 const randomIndex = Math.floor(Math.random() * unmatchedCards.length); cardIndex = unmatchedCards.splice(randomIndex, 1)[0]; } } // 翻开卡片 flipCardAutomatically(cardIndex); }, interval); } // 智能查找匹配对 function findSmartPair(unmatchedCards) { // 尝试找到一对匹配的卡片 for (let i = 0; i < unmatchedCards.length; i++) { const cardIndex1 = unmatchedCards[i]; const card1 = gameState.cards[cardIndex1]; for (let j = i + 1; j < unmatchedCards.length; j++) { const cardIndex2 = unmatchedCards[j]; const card2 = gameState.cards[cardIndex2]; if (card1.pairId === card2.pairId) { return [cardIndex1, cardIndex2]; } } } return null; // 没有找到匹配对 } // 自动翻开卡片 function flipCardAutomatically(index) { const cardElement = document.querySelector(`.card[data-index="${index}"]`); if (!cardElement) return; // 模拟点击翻开卡片 cardElement.classList.add('flipped'); gameState.flippedCards.push(index); // 检查是否匹配 if (gameState.flippedCards.length === 2) { setTimeout(checkMatch, 500); } } // 停止自动翻开 function stopAutoFlip() { if (gameState.autoFlipInterval) { clearInterval(gameState.autoFlipInterval); gameState.autoFlipInterval = null; gameState.autoFlipMode = null; } } // 页面加载完成后初始化游戏 window.addEventListener('DOMContentLoaded', initGame); </script> </body> </html> 总结为html整理出整体代码
07-20
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值