Today I had to fix a mysterious error on IE that “z-index: 200″ components appeared below “z-index: 1″. As always, this error was only seen on IE 6 through 8.
What I wanted to do was to have “balloon” div appear on top of the “li” when mouseover-ed. (switching css style display: none → visible)
↓Original html code:
<ul>
<li class="genre">
<a href="#"><span>見出し</span></a>
<div class="balloon">
<ul class="subcategory">
<li><a href="#">リンク</a></li>
<li><a href="#">リンク</a></li>
</ul>
</div>
</li>
... (same list - aligned as a vertical menu)
CSS style was something like this:
li.genre { position: relative; z-index: 1;}
li.genre a {display: block;}
div.balloon {
With this,
position: absolute; top:0;
left: 148px; z-index: 200;}
But div.balloon(z-index 200) appeared below li.genre(z-index 1).
As it turned out, for some reason IE adds up z-index for elements with “position:relative”. So if the first <li> was z-index 200, the second <li> was rendered as z-index 201, third, 202… and so on. That’s why the mouseover never worked.
Javascript solved the problem. It’s a code that reverse the order of z-index for each <li> element:
$(function(){
var categoryScope=$('div#categorySearch li.genre');
var categorySize = categoryScope.size();
categoryScope.each(function(i){
$(this).css('z-index',categorySize-1*i);
}).hover(function(){
$(this).children('div.balloon').show();
},function(){
$(this).children('div.balloon').hide();
});
});
“categorySize-1*i” does the magic.