最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript模擬實(shí)現(xiàn)新浪下拉菜單效果

 更新時(shí)間:2022年03月09日 11:06:31   作者:一夕ξ  
這篇文章主要為大家介紹了如何通過(guò)JavaScript模擬實(shí)現(xiàn)新浪的下拉菜單效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試

思考:首先在CSS布局上就出錯(cuò)了,導(dǎo)致后面設(shè)置JS時(shí)就有很大的問(wèn)題

<!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>評(píng)論</li>
                <li>@我</li>
            </ul>
        </div>
        <div class="nav2">
            <ul>
                <li>博客評(píng)論</li>
                <li>未讀提醒</li>
            </ul>
        </div>
        <div class="nav3">
            <ul>
                <li>免費(fèi)郵箱</li>
                <li>VIP郵箱</li>
                <li>企業(yè)郵箱</li>
                <li>新浪客戶郵箱</li>
            </ul>
        </div>
    </div>
    <script>
        //獲得導(dǎo)航欄元素
        var nav = document.querySelector('.nav')
            //注冊(cè)下拉事件點(diǎn)擊的時(shí)候,對(duì)應(yīng)的下拉菜單就是顯示的(一一對(duì)應(yīng))因此需要索引號(hào)
            //給na.children即下面的所有l(wèi)i設(shè)置自定義屬性
            //用不著,因?yàn)橄旅嫦吕藛味歼M(jìn)行了分別命名,但這樣就不能用循環(huán)了
        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')
            //獲取下拉菜單子元素
            //應(yīng)該用data-index來(lái)獲取
            // 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>

導(dǎo)航欄里面的li都要有鼠標(biāo)經(jīng)過(guò)的效果,所以需要循環(huán)注冊(cè)事件

<!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="">評(píng)論</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="">評(píng)論</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="">評(píng)論</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="">評(píng)論</a></li>
                <li><a href="">@我</a></li>
            </ul>
        </li>
    </ul>
    <script>
        //獲取元素
        var nav = document.querySelector('.nav')
        var lis = nav.children
            //循環(huán)注冊(cè)事件
        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>

未完成

注意用節(jié)點(diǎn)的方式獲取元素

總歸是完成了,對(duì)于js設(shè)置的時(shí)候,不太合理。

<!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 {
            /* 冒號(hào)hover前面不要加空格 */
            background-color: rgb(138, 129, 129);
        }
        
        .navv0>li:hover,
        .navv1>li:hover,
        .navv2>li:hover {
            /* 冒號(hào)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="">評(píng)論</a></li>
            <li><a href="">@我</a></li>
        </ul>
    </ul>
    <ul class="nav1">
        <li><a href="">博客</a></li>
        <ul class="navv1">
            <li><a href="">博客評(píng)論</a></li>
            <li><a href="">未讀提醒</a></li>
 
        </ul>
    </ul>
    <ul class="nav2">
        <li><a href="">郵箱</a></li>
        <ul class="navv2">
            <li><a href="">免費(fèi)郵箱</a></li>
            <li><a href="">VIP郵箱</a></li>
            <li><a href="">企業(yè)郵箱</a></li>
            <li><a href="">新浪客戶郵箱</a></li>
 
        </ul>
    </ul>
    <script>
        //鼠標(biāo)放在第一個(gè)大的nav中時(shí),下拉欄就顯示,離開(kāi)第一個(gè)大nav時(shí)后,下拉菜單就不顯示,這需要對(duì)第一個(gè)nav的盒子大小有要求,需要?jiǎng)偤冒褍?nèi)容
        //獲取元素
        var nav0 = document.querySelector('.nav0')
        var navv0 = document.querySelector('.navv0')
            //這三部分一起使用才行,首先鼠標(biāo)放在nav里面的第一個(gè)導(dǎo)航欄里面,下來(lái)菜單需要出現(xiàn),鼠標(biāo)點(diǎn)在第一個(gè)下拉菜單時(shí)候,要保持出現(xiàn),當(dāng)鼠標(biāo)離開(kāi)整個(gè)nav的時(shí)候,下拉菜單隱藏
        nav0.children[0].onmouseover = function() {
            navv0.style.display = 'block'
                // this.style.backgroundColor = 'rgb(211,211,211)' //沒(méi)必要這樣寫(xiě),直接寫(xiě)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>

到此這篇關(guān)于JavaScript模擬實(shí)現(xiàn)新浪下拉菜單效果的文章就介紹到這了,更多相關(guān)JavaScript下拉菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript中的函數(shù)申明、函數(shù)表達(dá)式、箭頭函數(shù)

    JavaScript中的函數(shù)申明、函數(shù)表達(dá)式、箭頭函數(shù)

    js中的函數(shù)可以通過(guò)幾種方式創(chuàng)建,具體創(chuàng)建方法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),文中通過(guò)例子給大家介紹了函數(shù)聲明和表達(dá)式之間的差別,感興趣的朋友跟隨小編一起看看吧
    2019-12-12
  • JavaScrip變量聲明關(guān)鍵字var、let、const詳解

    JavaScrip變量聲明關(guān)鍵字var、let、const詳解

    在JavaScript中var、let和const是用于聲明變量的關(guān)鍵字,但它們之間存在一些關(guān)鍵的區(qū)別,這篇文章主要介紹了JavaScrip變量聲明關(guān)鍵字var、let、const的相關(guān)資料,需要的朋友可以參考下
    2025-04-04
  • JavaScript實(shí)現(xiàn)Base64編碼轉(zhuǎn)換

    JavaScript實(shí)現(xiàn)Base64編碼轉(zhuǎn)換

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)Base64編碼轉(zhuǎn)換的相關(guān)資料,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2016-04-04
  • JavaScript函數(shù)中關(guān)于valueOf和toString的理解

    JavaScript函數(shù)中關(guān)于valueOf和toString的理解

    本文給大家介紹JavaScript函數(shù)中關(guān)于valueOf和toString的理解,簡(jiǎn)單的說(shuō)就是需要轉(zhuǎn)換為字符串時(shí),會(huì)調(diào)用toString,需要轉(zhuǎn)換為數(shù)字時(shí)需要調(diào)用valueOf。對(duì)js valueof tostring知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-06-06
  • js Dialog 去掉右上角的X關(guān)閉功能

    js Dialog 去掉右上角的X關(guān)閉功能

    用到 dialog彈出框時(shí),不想要右上角的X 關(guān)閉功能,只是做個(gè)提示信息顯示,下面是具體的去掉方法,大家可以參考下
    2014-04-04
  • javascript函數(shù)的4種調(diào)用方式與this的指向

    javascript函數(shù)的4種調(diào)用方式與this的指向

    本文主要介紹了javascript函數(shù)的4種調(diào)用方式與this(上下文)的指向,文中有詳細(xì)的代碼示例,感興趣的同學(xué)可以參考閱讀一下
    2023-05-05
  • Javascript中parseInt的正確使用方式

    Javascript中parseInt的正確使用方式

    今天小編就為大家分享一篇關(guān)于Javascript中parseInt的正確使用方式,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • 前端axios下載excel文件(二進(jìn)制)的處理方法

    前端axios下載excel文件(二進(jìn)制)的處理方法

    新接了項(xiàng)目,遇到這樣的需求,通過(guò)后端接口下載excel文件,后端沒(méi)有文件地址,返回二進(jìn)制流文件。接下來(lái)通過(guò)實(shí)例代碼給大家分享前端axios下載excel文件(二進(jìn)制)的處理方法,一起看看吧
    2018-07-07
  • 原生js+ajax分頁(yè)組件

    原生js+ajax分頁(yè)組件

    這篇文章主要為大家詳細(xì)介紹了原生js+ajax分頁(yè)組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • js中將HTMLCollection/NodeList/偽數(shù)組轉(zhuǎn)換成數(shù)組的代碼

    js中將HTMLCollection/NodeList/偽數(shù)組轉(zhuǎn)換成數(shù)組的代碼

    js中將HTMLCollection/NodeList/偽數(shù)組轉(zhuǎn)換成數(shù)組的代碼,需要的朋友可以參考下。
    2011-07-07

最新評(píng)論

南平市| 桑植县| 孙吴县| 宜春市| 梅河口市| 得荣县| 扎囊县| 明星| 河北省| 曲沃县| 翼城县| 册亨县| 贵阳市| 汤阴县| 婺源县| 邯郸市| 凤翔县| 龙门县| 海阳市| 昌平区| 兴义市| 黄山市| 嘉义市| 织金县| 长岭县| 神木县| 阳信县| 五原县| 宜阳县| 华坪县| 新郑市| 舟曲县| 全州县| 黔东| 平度市| 邯郸县| 利川市| 抚州市| 黔西县| 墨竹工卡县| 楚雄市|