ie下z-index问题的解决方法

最近做了一个项目,用到多步骤表单,但是表单的进度条在ie下确显示不正常,如下图


下面是进度条的代码:

<span style="font-size:14px;"><ul id="progressbar">
<span>	</span><li class="active">View Info</li>
	<li>Filters</li>
	<li>Column Settings</li>
	<li>Finish</li>
</ul></span>

<span style="font-size:14px;">#progressbar li {
	list-style-type: none;
	text-transform: uppercase;
	font-size: 9px;
	float: left;
	position: relative;
	text-align:center;
	counter-increment: step;
}
#progressbar li:before {
	content: counter(step);
	width: 20px;
	line-height: 20px;
	display: block;
	font-size: 10px;
	color: #333;
	background: #eee;
	border-radius: 3px;
	margin: 0 auto 5px auto;
	text-align:center;
}
/*progressbar connectors*/
#progressbar li:after {
	content: '';
	width: 100%;
	height: 2px;
	background: #eee;
	position: absolute;
	left: -50%;
	top: 9px;
	z-index: -1; /*put it behind the numbers*/
}
#progressbar li:first-child:after {
	/*connector not needed before the first step*/
	content: none;
}
/*marking active/completed steps green*/
/*The number of the step and the connector before it = green*/
#progressbar li.active:before,  #progressbar li.active:after{
	background: #27AE60;
	color: #fff;
}
 

可以看见灰色的连接线(li:after)覆盖了li中的数字,但是明明已经设置了z-index为-1了啊,为什么没有用呢?原因是在ie中z-index仅仅作用于父元素中,li没有设置z-index在ie中默认为0,那么所有的li的z-index都是一样的,因此后一个li中的所有内容都会覆盖前一个li,所以后一个li的连接线覆盖了前一个li的数字.因此这种情况下我们需要给父元素设置不同的z-index.

解决办法如下:

$('#progressbar li').each(function(idx,li){
	$(li).css('z-index',100-idx);
});
这样,四个li的z-index分别为100,99,98,97,就不会覆盖到前面的li了.

关于这个问题的更详细的解释请看:

http://www.davidtong.me/z-index-misconceptions-bugs-fixes/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当设置了父级元素的z-index属性后,子元素的z-index是相对于父级元素的index。因此,父级元素的z-index优先于子元素的z-index。这意味着如果父级元素设置了z-index,子元素的z-index设置可能会失效。这情况通常发生在父元素和子元素都具有定位属性(position: relative或position: absolute)时。在这情况下,可以尝试将子元素的z-index值设置为负值,以解决z-index失效的问题。 综上所述,当父元素具有定位属性并设置了z-index时,子元素的z-index可能会失效。此时可以尝试将子元素的z-index设置为负值来解决问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [css z-index无效](https://blog.csdn.net/weixin_43478592/article/details/115394398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ie7下z-index失效问题解决方法(详细分析)](https://download.csdn.net/download/weixin_38519082/13982881)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值