CSS轮播图中图片链接无法点击的问题排查求助
CSS轮播图图片链接无法点击的解决方案
问题分析
核心问题有两点:
- 底层图片未设置定位属性,
z-index仅对定位元素生效,因此之前的层级调整无效 - 所有轮播项
.item为绝对定位叠加,即使opacity:0,元素依然会接收点击事件,拦截上层链接的交互
解决方案
1. 给底层图片添加定位,让z-index生效
修改.item img样式,补充定位属性,确保层级调整生效:
.item img { position: relative; z-index: 1; }
2. 给链接容器设置更高层级
直接给所有包含链接的span设置更高z-index,确保它们始终在底层图片之上:
.span1aa, .span1a, .span1 { position: absolute; z-index: 2; margin: 0; transform: translate(-50%, -50%); }
3. 禁止透明轮播项接收点击事件
给.item添加pointer-events: none,并在轮播项显示时(opacity:1)恢复点击,避免透明项拦截上层交互:
.colorDissolve .item { position: absolute; text-align: center; opacity: 0; pointer-events: none; /* 透明时不响应点击 */ animation: colorDissolve 21s linear infinite; } /* 在动画关键帧中同步控制点击权限 */ @keyframes colorDissolve { 0%, 25%, 100% { opacity: 0; pointer-events: none; } 4.17%, 20.84% { opacity: 1; pointer-events: auto; } }
修改后的完整CSS代码
/* colorDissolve */ .colorDissolve { position: relative; max-width: 95vh; height: 76vh; left: 50%; transform: translateX(-50%); } .colorDissolve .item { position: absolute; text-align: center; opacity: 0; pointer-events: none; -webkit-animation: colorDissolve 21s linear infinite; -moz-animation: colorDissolve 21s linear infinite; -ms-animation: colorDissolve 21s linear infinite; animation: colorDissolve 21s linear infinite; } .colorDissolve .item:nth-child(2) { -webkit-animation-delay: 9s; -moz-animation-delay: 9s; -ms-animation-delay: 9s; animation-delay: 9s; } .colorDissolve .item:nth-child(3) { -webkit-animation-delay: 15s; -moz-animation-delay: 15s; -ms-animation-delay: 15s; animation-delay: 15s; } .span1aa, .span1a, .span1 { position: absolute; z-index: 2; margin: 0; transform: translate(-50%, -50%); } .span1aa { top: 25%; left: 27%; } .span1aa a img { width: 66%; } .item img { position: relative; z-index: 1; } .span1a { top: 71%; left: 27%; } .span1a a img { width: 62%; } .span1 { top: 30%; left: 72%; } .span1 a img { width: 95%; } @-webkit-keyframes colorDissolve { 0%, 25%, 100% { opacity: 0; pointer-events: none; } 4.17%, 20.84% { opacity: 1; pointer-events: auto; } } @-moz-keyframes colorDissolve { 0%, 25%, 100% { opacity: 0; pointer-events: none; } 4.17%, 20.84% { opacity: 1; pointer-events: auto; } } @-ms-keyframes colorDissolve { 0%, 25%, 100% { opacity: 0; pointer-events: none; } 4.17%, 20.84% { opacity: 1; pointer-events: auto; } } @keyframes colorDissolve { 0%, 25%, 100% { opacity: 0; pointer-events: none; } 4.17%, 20.84% { opacity: 1; pointer-events: auto; } }
效果说明
修改后,链接容器的层级始终高于底层图片,同时透明状态的轮播项不再拦截点击事件,链接即可正常响应点击。
内容的提问来源于stack exchange,提问作者Emily
相关产品推荐
相关产品推荐

