最近写CSS3和js结合,遇到了很多次z -i ndex不 生效 的情况:
1.在用z-index的时候,该元素没有定位( stat ic定位除外)
2.在有定位的情况下,该元素的z-index没有生效,是因为该元素的子元素后来居上,盖住了该元素,解决方式:将盖住该元素的子元素的z-index设置为负数
下拉框例子:
1.盖住的时候:
2.将下拉框的z-index设置为负数
代码:
<!DOCTY PE HT ML P ub LIC "-//W3C//DTD HTML 4.01 Trans IT ional//EN" "http://HdhCmsTestw3. org /TR/html4/loose.dtd">
<html>
<head>
< ;m eta http-equiv="Content -t ype" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style type="text/css">
* {
padding: 0;
m arg in: 0;
list -s tyle: none;
}
.all {
width: 330px;
h ei ght: 120px;
overflow: hidden;
background: url( img /bg. jpg ) no-repeat;
mar gin : 100px auto;
line-height: 30px;
text-align: center ;
padding-left: 10px;
margin-bottom: 0;
}
.all ul {
position: relative;
height: 30px;
width: 100%;
}
.all ul li {
width: 100px;
height: 30px;
background: url(img/libg.jpg);
float: left;
margin-right: 10px;
position: relative;
cursor: pointer;
}
.all ul ul {
position: absolute;
left: 0;
top:-90px;
/*dis play : none; 是一瞬间的事*/
transition: all 1s;
opacity: 0;
/*后来的 盒子 会盖住前面的盒子,就算前面的盒子z-index再大也会被盖住,
不过 可以设置后来的盒子的z-index为负数就行了*/
z-index:-1;
}
.all ul .lvTow {
top:-90px;
opacity: 0;
}
.all ul .show{
top:30px;
opacity: 1;
}
</style>
</head>
<body>
<div class="all" id="list">
<ul>
<li>一级菜单
<ul >
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单
<ul >
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
<li>一级菜单
<ul >
<li>二级菜单</li>
<li>二级菜单</li>
<li>二级菜单</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
<script>
// 获取对象 遍历对象操作 显示模块 隐藏模块
function List(id) { // 获取对象
this.id = document.getElementById(id);
// 取 id 值
this.lis = this.id.children[0].children; // 获取一级菜单所有的li
}
// init 初始化
List. PR ototype.init = function() { // 遍历所有的li 显示和隐藏
VAR t hat = this;
for(var i=0;i<this.lis.length; i++ )
{
this.lis[i].index = i;
this.lis[i].onmouseover = function() {
that.show(this.children[0]); // 显示出来
}
this.lis[i].onmouseout = function() {
that.hide(this.children[0]); // 隐藏起来
}
}
}
// 显示模块
List.prototype.show = function(obj) {
// obj.style.display = "block";
obj. classname = "show";
}
// 隐藏模块
List.prototype.hide = function(obj) {
// obj.style.display = "none";
obj.classN am e = "lvTow";
}
var list = new List("list"); // 实例化了一个对象 叫 list
list.init();
</script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。&nbs p;
总结
以上是 为你收集整理的 CSS3关于z-index不生效问题的解决 全部内容,希望文章能够帮你解决 CSS3关于z-index不生效问题的解决 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
查看更多关于CSS3关于z-index不生效问题的解决的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did201026