简体   繁体   English

Z-index渲染链接不可点击

[英]Z-index rendering links unclickable

My negative z-index is causing the links to be unclickable even though there are no other elements above it. 我的负Z索引导致链接不可点击,即使它上面没有其他元素也是如此。 I can't figure out what is causing the issue. 我不知道是什么原因造成的。 I've removed all other elements on the page and it still exists happens. 我已经删除了页面上的所有其他元素,但它仍然存在。 What is covering the links? 涵盖链接的内容是什么?

 #scrollingdiv { width: 100%; margin-top: 50px; overflow: scroll; height: 200px; white-space: nowrap; } .boxes { height: 300px; width: 100px; background-color: green; display: inline-block; z-index:-2; position:relative; } 
 <div id='scrollingdiv'> <div style='margin-top:40px'></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <br/><a href='#'>link</a> </div> 

Update 更新资料

Just add a wrapper around the .boxes and add overflow: scroll; 只需在.box周围添加包装器并添加溢出: and height to it :) 和高度:)

 #scrollingdiv { width: 100%; margin-top: 50px; height: 200px; white-space: nowrap; } .boxes-wrap{ overflow: scroll; height: 150px; } #banner { width: 5000px; height: 50px; background-color: red; /* z-index:-1; */ } .boxes { height: 300px; width: 100px; background-color: green; display: inline-block; /* z-index:-2; position:relative; */ } 
 <div id='scrollingdiv'> <div id='banner'>HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER HEADER</div> <div class="boxes-wrap"> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <div class='boxes'>box<br/><a href='#'>link</a></div> <br/><a href='#'>link</a> </div> </div> 

This behavior is not limited to a elements. 这种行为不仅限于a元素。 You'll have the same problem if you change them to buttons. 如果将它们更改为按钮,则会遇到相同的问题。 Basically, you can see elements with a negative z-index, but you cannot interact with them. 基本上,您可以看到Z索引为负的元素,但是您无法与它们进行交互。 (See http://jsfiddle.net/bhxem01j/1/ for an example using buttons.) (有关使用按钮的示例,请参见http://jsfiddle.net/bhxem01j/1/ 。)

A quick fix may be to apply a z-index to all elements, then a smaller (but non-negative) z-index to .boxes : 快速解决方案可能是将z-index应用于所有元素,然后将较小(但非负数)的z-index应用于.boxes

* {
  ...
  position: relative;
  z-index: 1;
}

.boxes {
  ...
  z-index: 0;
}

Since z-index applies to positioned elements only, I added position: relative to the * styles. 由于z-index仅适用于定位的元素,因此我添加了position: relative对于*样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM