欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CSS3關(guān)于z-index不生效問題的解決

  發(fā)布時間:2020-02-19 16:31:31   作者:QinXiao.Shou   我要評論
這篇文章主要介紹了CSS3關(guān)于z-index不生效問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近寫CSS3和js結(jié)合,遇到了很多次z-index不生效的情況:

1.在用z-index的時候,該元素沒有定位(static定位除外)

2.在有定位的情況下,該元素的z-index沒有生效,是因為該元素的子元素后來居上,蓋住了該元素,解決方式:將蓋住該元素的子元素的z-index設(shè)置為負(fù)數(shù)

下拉框例子:

1.蓋住的時候:

2.將下拉框的z-index設(shè)置為負(fù)數(shù)

代碼:

<!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>無標(biāo)題文檔</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再大也會被蓋住,
    不過可以設(shè)置后來的盒子的z-index為負(fù)數(shù)就行了*/
    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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論