首頁 > 軟體

JavaScript模擬實現新浪下拉式選單效果

2022-03-09 13:00:10

思考:首先在CSS佈局上就出錯了,導致後面設定JS時就有很大的問題

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .nav {
            background-color: rgb(235, 225, 225);
            line-height: 30px;
            height: 30px;
            position: relative;
        }
        
        ul {
            margin: 0px 0px;
            padding: 0 0 0 0;
        }
        
        .nav1 li,
        .nav2 li,
        .nav3 li {
            display: block;
            padding-left: 10px;
            height: 20px;
            padding-top: 5px;
            padding-bottom: 5px;
            border: 1px solid orange;
            margin-top: -1px;
        }
        
        .nav1,
        .nav2,
        .nav3 {
            display: none
        }
        
        .nav1 {
            background-color: white;
            width: 80px;
            position: absolute;
            top: 0px;
            left: 0px
        }
        
        .nav2 {
            background-color: white;
            width: 100px;
            position: absolute;
            top: 0px;
            left: 80px
        }
        
        .nav3 {
            background-color: white;
            width: 120px;
            position: absolute;
            top: 0px;
            left: 160px
        }
        
        .nav div {
            width: 80px;
            text-align: center;
            line-height: 30px;
            float: left
        }
        
        .tort {
            position: relative;
            left: 0px
        }
        
        .se {
            background-color: rgb(201, 192, 192);
            color: orange
        }
        
        ul li:hover {
            background-color: orange;
        }
    </style>
</head>
 
<body>
    <div class="nav">
        <div>微博</div>
        <div>部落格</div>
        <div>郵箱</div>
    </div>
    <div class="tort">
        <div class="nav1">
            <ul>
                <li>私信</li>
                <li>評論</li>
                <li>@我</li>
            </ul>
        </div>
        <div class="nav2">
            <ul>
                <li>部落格評論</li>
                <li>未讀提醒</li>
            </ul>
        </div>
        <div class="nav3">
            <ul>
                <li>免費郵箱</li>
                <li>VIP郵箱</li>
                <li>企業郵箱</li>
                <li>新浪客戶郵箱</li>
            </ul>
        </div>
    </div>
    <script>
        //獲得導航欄元素
        var nav = document.querySelector('.nav')
            //註冊下拉事件點選的時候,對應的下拉式選單就是顯示的(一一對應)因此需要索引號
            //給na.children即下面的所有li設定自定義屬性
            //用不著,因為下面下拉式選單都進行了分別命名,但這樣就不能用迴圈了
        nav.children[0].setAttribute('data-index', '0')
        nav.children[1].setAttribute('data-index', '1')
        nav.children[2].setAttribute('data-index', '2')
        var nav1 = document.querySelector('.nav1')
        var nav2 = document.querySelector('.nav2')
        var nav3 = document.querySelector('.nav3')
            //獲取下拉式選單子元素
            //應該用data-index來獲取
            // var tort = document.querySelector('.tort')
            // nav1.setAttribute('data-idn', '0')
            // nav2.setAttribute('data-idn', '1')
            // nav3.setAttribute('data-idn', '2')
 
        // var nn = 
        // console.log(nn)
        //新增事件
        for (var i = 0; i < nav.children.length; i++) {
            nav.children[i].onmouseover = function() {
                this.className = 'se'
            }
            nav.children[i].onmouseout = function() {
                    this.className = ''
                }
                //新增下拉式選單顯示屬性
 
        }
        // nav.children[0].onmouseover = function() {
        //     nav1.style.display = 'block'
        //     nav2.style.display = ''
        //     nav3.style.display = ''
        // }
        // nav.children[1].onmouseover = function() {
        //     nav2.style.display = 'block'
        //     nav1.style.display = ''
        //     nav3.style.display = ''
        // }
        // nav.children[2].onmouseover = function() {
        //     nav3.style.display = 'block'
        //     nav2.style.display = ''
        //     nav1.style.display = ''
        // }
    </script>
 
</body>
 
</html>

導航欄裡面的li都要有滑鼠經過的效果,所以需要回圈註冊事件

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .nav li {
            list-style: none;
        }
        
        .nav>li>a:hover {
            background-color: #eee;
        }
        
        .nav ul {
            display: block;
            position: absolute;
            top: 41px;
            left: 0px;
            width: 100%;
            border-left: 1px solid #fecc5b;
            border-right: 1px solid #fecc5b
        }
        
        .nav ul li {
            border-bottom: 1px solid #fecc5b;
        }
        
        .nav ul li a:hover {
            background-color: #fff5da;
        }
        
        .m1 {
            position: absolute;
            top: 0px;
            left: 0px;
        }
        
        .m1 {
            position: absolute;
            top: 0px;
            left: 20px;
        }
    </style>
</head>
 
<body>
    <ul class="nav">
        <li>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >微博</a>
            <ul class="m1">
                <li><a href="">私信</a>
                </li>
                <li><a href="">評論</a></li>
                <li><a href="">@我</a></li>
 
            </ul>
        </li>
        <li>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >微博</a>
            <ul>
                <li><a href="">私信</a>
                </li>
                <li><a href="">評論</a></li>
                <li><a href="">@我</a></li>
            </ul>
        </li>
        <li>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >微博</a>
            <ul class="m1">
                <li><a href="">私信</a>
                </li>
                <li><a href="">評論</a></li>
                <li><a href="">@我</a></li>
            </ul>
        </li>
        <li>
            <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >微博</a>
            <ul>
                <li><a href="">私信</a>
                </li>
                <li><a href="">評論</a></li>
                <li><a href="">@我</a></li>
            </ul>
        </li>
    </ul>
    <script>
        //獲取元素
        var nav = document.querySelector('.nav')
        var lis = nav.children
            //迴圈註冊事件
        for (var i = 0; i < lis.length; i++) {
            lis[i].onmouseover = function() {
                this.children[1].style.display = 'block'
 
            }
            lis[i].onmouseout = function() {
                this.children[1].style.display = ''
 
            }
        }
    </script>
</body>
 
</html>

未完成

注意用節點的方式獲取元素

總歸是完成了,對於js設定的時候,不太合理。

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            list-style: none;
            height: 20px;
            line-height: 20px;
        }
        
        a {
            text-decoration: none;
            color: black
        }
        
        .nav0,
        .nav1,
        .nav2 {
            position: relative;
            width: 80px;
            height: 82px;
            padding-left: 0px;
            float: left
        }
        
        .nav0>li,
        .nav1>li,
        .nav2>li {
            background-color: rgb(221, 216, 216);
            text-align: center;
        }
        
        .navv0,
        .navv1,
        .navv2 {
            position: absolute;
            top: 20px;
            left: 0px;
            border-top: 0px;
            padding-left: 0px;
            width: 80px;
            margin-top: -1px;
            display: none
        }
        
        .navv1 {
            width: 100px
        }
        
        .navv2 {
            width: 120px
        }
        
        .navv0 li,
        .navv1 li,
        .navv2 li {
            border-bottom: 1px solid orange;
            border-left: 1px solid orange;
            border-right: 1px solid rgb(240, 169, 28);
            padding-left: 5px
        }
        
        li:hover a {
            /* 注意改變的是連結裡面的文字顏色 */
            color: orange
        }
        
        .nav0>li:hover,
        .nav1>li:hover,
        .nav2>li:hover {
            /* 冒號hover前面不要加空格 */
            background-color: rgb(138, 129, 129);
        }
        
        .navv0>li:hover,
        .navv1>li:hover,
        .navv2>li:hover {
            /* 冒號hover前面不要加空格 */
            background-color: rgb(236, 232, 203);
        }
    </style>
</head>
 
<body>
 
    <ul class="nav0">
        <li><a href="">微博</a></li>
        <ul class="navv0">
            <li><a href="">私信</a></li>
            <li><a href="">評論</a></li>
            <li><a href="">@我</a></li>
        </ul>
    </ul>
    <ul class="nav1">
        <li><a href="">部落格</a></li>
        <ul class="navv1">
            <li><a href="">部落格評論</a></li>
            <li><a href="">未讀提醒</a></li>
 
        </ul>
    </ul>
    <ul class="nav2">
        <li><a href="">郵箱</a></li>
        <ul class="navv2">
            <li><a href="">免費郵箱</a></li>
            <li><a href="">VIP郵箱</a></li>
            <li><a href="">企業郵箱</a></li>
            <li><a href="">新浪客戶郵箱</a></li>
 
        </ul>
    </ul>
    <script>
        //滑鼠放在第一個大的nav中時,下拉欄就顯示,離開第一個大nav時後,下拉式選單就不顯示,這需要對第一個nav的盒子大小有要求,需要剛好把內容
        //獲取元素
        var nav0 = document.querySelector('.nav0')
        var navv0 = document.querySelector('.navv0')
            //這三部分一起使用才行,首先滑鼠放在nav裡面的第一個導航欄裡面,下來選單需要出現,滑鼠點在第一個下拉式選單時候,要保持出現,當滑鼠離開整個nav的時候,下拉式選單隱藏
        nav0.children[0].onmouseover = function() {
            navv0.style.display = 'block'
                // this.style.backgroundColor = 'rgb(211,211,211)' //沒必要這樣寫,直接寫hover屬性即可
                // this.style.color = 'red'
 
        }
        navv0.onmouseover = function() {
            navv0.style.display = 'block'
                // nav0.children[0].style.backgroundColor = 'rgb(211,211,211)'
        }
        nav0.onmouseout = function() {
                navv0.style.display = ''
                    // nav0.children[0].style.backgroundColor = 'rgb(221, 216, 216)'
            }
            // for (var i = 0; i < navv0.children; i++) {
            //     navv0.children[i].onmouseover = function() {
            //         console.log(11)
            //             // this.style.backgroundColor = 'orange'
            //     }
            // }
 
 
        var nav1 = document.querySelector('.nav1')
        var navv1 = document.querySelector('.navv1')
        nav1.children[0].onmouseover = function() {
            navv1.style.display = 'block'
                // this.style.backgroundColor = 'rgb(211,211,211)'
 
        }
        navv1.onmouseover = function() {
            navv1.style.display = 'block'
                // nav1.children[0].style.backgroundColor = 'rgb(211,211,211)'
        }
        nav1.onmouseout = function() {
            navv1.style.display = ''
                // nav1.children[0].style.backgroundColor = 'rgb(221, 216, 216)'
        }
 
        var nav2 = document.querySelector('.nav2')
        var navv2 = document.querySelector('.navv2')
        nav2.children[0].onmouseover = function() {
            navv2.style.display = 'block'
                // this.style.backgroundColor = 'rgb(211,211,211)'
 
        }
        navv2.onmouseover = function() {
            navv2.style.display = 'block'
                // nav1.children[0].style.backgroundColor = 'rgb(211,211,211)'
        }
        nav2.onmouseout = function() {
            navv2.style.display = ''
                // nav1.children[0].style.backgroundColor = 'rgb(221, 216, 216)'
        }
    </script>
</body>
 
</html>

到此這篇關於JavaScript模擬實現新浪下拉式選單效果的文章就介紹到這了,更多相關JavaScript下拉式選單內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com