导航栏展开后名称动画遮挡导航按钮问题求助
解决导航栏与名称动画的层级冲突问题
问题原因
导航栏打开后无法点击按钮,是因为名称动画#name_shine的堆叠层级高于导航栏:
#name_shine使用position: relative,且在DOM结构中位于导航元素之后,默认堆叠优先级更高- 导航容器
.wrapper虽为固定定位,但未显式设置z-index,被上方的动画文本完全遮挡,导致点击事件无法触发
解决方案
给导航容器设置更高的z-index,确保其堆叠在名称动画上方,只需修改一处CSS:
在.wrapper的样式中添加z-index: 10:
.wrapper{ position: fixed; top: 0; left: 0; height: 100%; width: 100%; background: linear-gradient(-135deg, #000000, #ffffff); clip-path: circle(25px at calc(100% - 45px) 45px); transition: all 0.3s ease-in-out; z-index: 10; /* 新增这一行 */ }
修改后的完整代码
<!DOCTYPE html> <html lang="no"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Hjemmeside</title> <style> @import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap'); *{ margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; } .wrapper{ position: fixed; top: 0; left: 0; height: 100%; width: 100%; background: linear-gradient(-135deg, #000000, #ffffff); clip-path: circle(25px at calc(100% - 45px) 45px); transition: all 0.3s ease-in-out; z-index: 10; /* 新增z-index */ } #active:checked ~ .wrapper{ clip-path: circle(75%); } .menu-btn{ position: absolute; z-index: 2; right: 20px; top: 20px; height: 50px; width: 50px; text-align: center; line-height: 50px; border-radius: 50%; font-size: 20px; color: #fff; cursor: pointer; background: linear-gradient(-135deg, #000000, #ffffff); transition: all 0.3s ease-in-out; } #active:checked ~ .menu-btn{ background: #fff; color: #1c2541; } #active:checked ~ .menu-btn i:before{ content: "\f00d"; } .wrapper ul{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); list-style: none; text-align: center; } .wrapper ul li{ margin: 15px 0; } .wrapper ul li a{ color: none; text-decoration: none; font-size: 30px; font-weight: 500; padding: 5px 30px; color: #fff; position: relative; line-height: 50px; transition: all 0.3s ease; } .wrapper ul li a:after{ position: absolute; content: ""; background: #fff; width: 100%; height: 50px; left: 0; border-radius: 50px; transform: scaleY(0); z-index: -1; transition: transform 0.3s ease; } .wrapper ul li a:hover:after{ transform: scaleY(1); } .wrapper ul li a:hover{ color: #000000; } input[type="checkbox"]{ display: none; } .content{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: -1; text-align: center; width: 100%; color: #202020; } .content .title{ font-size: 40px; font-weight: 700; } .content p{ font-size: 35px; font-weight: 600; } /*Name shine*/ body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #000; } #name_shine { position: relative; font-family: sans-serif; text-transform: uppercase; font-size: 15vw; letter-spacing: 4px; overflow: hidden; background: linear-gradient(90deg, #000, #ffffff, #000); background-repeat: no-repeat; background-size: 80%; animation: animate 3s linear infinite; background-clip: border-box; -webkit-background-clip: text; -webkit-text-fill-color: rgba(255, 255, 255, 0); } @keyframes animate { 0% { background-position: -500%; } 100% { background-position: 500%; } } </style> </head> <body> <div> <input type="checkbox" id="active" /> <label for="active" class="menu-btn"><i class="fas fa-bars"></i></label> <div class="wrapper"> <ul> <li><a href="index.html">Hjem</a></li> <li><a href="hobbyer.html">Hobbyer</a></li> <li><a href="kommune.html">Kommune</a></li> <li><a href="testpage.html">Test Page</a></li> <li><a href="#">Bildegalleri</a></li> </ul> </div> </div> <p id="name_shine">Lukas Heen Berge</p> <script> </script> </body> </html>
原理说明
CSS堆叠上下文由position(非static值)和z-index共同控制。给.wrapper设置z-index:10后,其堆叠层级会高于默认层级的#name_shine,导航按钮就能正常接收点击事件。
内容的提问来源于stack exchange,提问作者lukashb
相关产品推荐
相关产品推荐

