简体   繁体   English

奇怪的z-index行为阻止鼠标交互:bug还是正常?

[英]Weird z-index behaviour preventing mouse interactions: bug or normal?

Every time I try to use z-index in a webpage to change the order of stacking overlapping divs, I seem to run into a problem where the div that is being forced lower becomes unresponsive to mouse events. 每当我尝试在网页中使用z-index来改变堆叠重叠div的顺序时,我似乎遇到了一个问题,即被迫降低的div对鼠标事件没有反应。

In my current situation I have: 在我目前的情况下,我有:

<div class="leftcolumn">
<div class="leftbar"></div> <!-- a 95px wide bar on the left -->
...
<h3>header</h3> <!-- a little header sitting inside the leftbar
...
</div>

By default the h3 isn't showing - it's hidden behind the leftbar. 默认情况下,h3没有显示 - 它隐藏在左侧栏后面。 If I add z-index: 5; 如果我添加z-index:5; to the h3, it still doesn't show. 到了H3,它仍然没有显示出来。

So I add z-index: -1 to the leftbar. 所以我在左栏添加了z-index:-1。 Now it's hidden behind the leftcolumn - but at least h3 shows. 现在它隐藏在左栏后面 - 但至少h3显示。

So I add z-index: -2 to the leftcolumn. 所以我在左栏添加了z-index:-2。 Now everything looks right - but you can't click on anything inside leftcolumn. 现在一切看起来都正确 - 但你不能点击左栏内的任何东西。 The mouse cursor doesn't change from an arrow. 鼠标光标不会从箭头改变。

I get this exact behaviour in both Chrome and Firefox. 我在Chrome和Firefox中都得到了这种确切的行为。 IE7 doesn't show the leftbar at all, but at least stuff is clickable. IE7根本不显示左栏,但至少可以点击。

So, am I misunderstanding z-index, or is there a bug in both FF and Chrome here? 那么,我是否误解了z-index,或者这里的FF和Chrome都存在错误? Can z-index be effectively used for this kind of stuff, or do I have to find another way? z-index可以有效地用于这种东西,还是我必须找到另一种方式?

(I can change the HTML, but the less, the better.) (我可以更改HTML,但越少越好。)

Ok, 10 seconds later I discover that using only positive z-index'es makes the problem go away. 好吧,10秒后,我发现只使用正面的z-index'会使问题消失。 Perhaps negative z-index means the object is below the level that the mouse cursor notionally lives? 也许负面的z-index意味着对象低于鼠标光标在理论上生活的水平?

Do you know that in order for z-index to work right, you need to position your elements, even if they're simply position: relative (which doesn't change their position any but allows you to use z-index). 你知道吗?为了使z-index正常工作,你需要定位你的元素,即使它们只是position: relative (它不会改变它们的位置,但允许你使用z-index)。 That way, you should be able to give leftbar a position of, say, 2 and your h3 a position of, say, 3 . 这样,你应该能够给leftbar的位置,例如2 ,你的h3的位置,比方说3 And your h3 should be on top. 你的h3应该是最重要的。

You can use any position type as long as you have one. 您可以使用任何位置类型,只要您有一个。

For recap: 回顾:

#leftcolumn { position: absolute; z-index: 1; }

#leftbar { position: relative; z-index: 2; }

h3 { position: relative; z-index: 3; }

Even though the leftcolumn content is visible, the leftbar div is now sitting on top of it, likely with a transparent background. 即使左栏内容可见,左栏div现在也位于其上方,可能具有透明背景。 Ideally you would want to modify the HTML so that the H3 resides within the leftbar, but if that is not an option, you may need to apply z-index to specific elements within the leftcolumn in order to pull them above elements in the leftbar. 理想情况下,您可能希望修改HTML以使H3位于左侧栏中,但如果这不是一个选项,您可能需要将z-index应用于左列中的特定元素,以便将它们拉到左侧栏中的元素之上。

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

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