小編給大家分享一下CSS3怎么解決z-index不生效的問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創新互聯公司專注于企業全網整合營銷推廣、網站重做改版、雄縣網站定制設計、自適應品牌網站建設、H5高端網站建設、商城建設、集團公司官網建設、成都外貿網站建設、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為雄縣等各大城市提供網站開發制作服務。
最近寫CSS3和js結合,遇到了很多次z-index不生效的情況:
1.在用z-index的時候,該元素沒有定位(static定位除外)
2.在有定位的情況下,該元素的z-index沒有生效,是因為該元素的子元素后來居上,蓋住了該元素,解決方式:將蓋住該元素的子元素的z-index設置為負數
下拉框例子:
1.蓋住的時候:

2.將下拉框的z-index設置為負數

代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
list-style: none;
}
.all {
width: 330px;
height: 120px;
overflow: hidden;
background: url(img/bg.jpg) no-repeat;
margin: 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;
/*display: 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.prototype.init = function() { // 遍歷所有的li 顯示和隱藏
var that = 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.className = "lvTow";
}
var list = new List("list"); // 實例化了一個對象 叫 list
list.init();
</script>以上是“CSS3怎么解決z-index不生效的問題”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯行業資訊頻道!
文章名稱:CSS3怎么解決z-index不生效的問題
文章路徑:http://www.yijiale78.com/article18/gddhdp.html
成都網站建設公司_創新互聯,為您提供軟件開發、移動網站建設、App設計、網站導航、品牌網站設計、外貿網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯