CSS星空背景动画导致页面href链接失效的问题求助
问题描述
给HTML页面添加CSS星空背景动画后,所有href书签及外部链接均无法点击。移除动画元素(HTML第7-13行)后链接恢复正常,调整动画元素位置无效,求解决办法。
问题原因
绝对定位的#space和.stars元素铺满了整个视口,且设置了较高的z-index:99——即便元素处于透明状态,也会阻挡下方元素的鼠标点击事件,导致链接无法被触发。
解决方案
给星空容器添加pointer-events: none;属性,让它不拦截任何鼠标事件,事件会直接穿透到下方的链接元素上。同时修正HTML中的标签语法错误。
修改后的HTML代码
<html> <br> <br> <div id="space"> <div class="stars"></div> <div class="stars"></div> <div class="stars"></div> <div class="stars"></div> <div class="stars"></div> </div> <div> <p style="color:orange;background-color:green;text-align:center;font-size:55px;">Page Title</p> <p style="color:white;text-align:center;font-size:25;">this is what it's about</p> <br> <br> <p style="color:orange;text-align:center;font-size:35;">I can add a link right <a href="#new_section">Here</a></p> </div> <br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br><br> <br> <p style="color:orange;text-align:center;font-size:35;">this is a bookmark</p> <a id="new_section">new section</a> </html>
修改后的CSS代码
body { background: #000; } /* Stars ------------------------------------- */ #space, .stars { overflow: hidden; position: absolute; top: 0; bottom: 0; z-index: 99; left: 0; right: 0; pointer-events: none; /* 添加这行实现事件穿透 */ } .stars { background-image: radial-gradient(2px 2px at 20px 30px, #eee, rgba(0,0,0,0)), radial-gradient(2px 2px at 40px 70px, #fff, rgba(0,0,0,0)), radial-gradient(2px 2px at 50px 160px, #ddd, rgba(0,0,0,0)), radial-gradient(2px 2px at 90px 40px, #fff, rgba(0,0,0,0)), radial-gradient(2px 2px at 130px 80px, #fff, rgba(0,0,0,0)), radial-gradient(2px 2px at 160px 120px, #ddd, rgba(0,0,0,0)); background-repeat: repeat; background-size: 200px 200px; animation: zoom 5s infinite; opacity: 0; } .stars:nth-child(1) { background-position: 50% 50%; animation-delay: 0s; } .stars:nth-child(2) { background-position: 20% 60%; animation-delay: 1s; } .stars:nth-child(3) { background-position: -20% -30%; animation-delay: 2s; } .stars:nth-child(4) { background-position: 40% -80%; animation-delay: 3s; } .stars:nth-child(5) { background-position: -20% 30%; animation-delay: 4s; } @keyframes zoom { 0% { opacity: 0; transform: scale(0.5); animation-timing-function: ease-in; } 85% { opacity: .75; transform: scale(2.8); animation-timing-function: linear; } 100% { opacity: 0; transform: scale(3.5); } }
额外说明
pointer-events: none;会让元素完全忽略鼠标事件(点击、悬停等),事件会直接作用于其下方的元素。- 修正了HTML中错误的
div>标签为<div>,避免页面结构混乱。
内容的提问来源于stack exchange,提问作者mike_p
相关产品推荐
相关产品推荐

