下拉菜单js

css随便设置的,需要再额外修改即可

<!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>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    body{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
    ul{
        list-style: none;
    }
    .nav{
        display: flex;
        justify-content: space-around;
        width: 500px;
        height: 50px;
        line-height: 50px;
        background-color: pink;
    }
    a{  display: inline-block;
        width: 100%;
        text-decoration: none;
        color: #fff;
    }
    .nav li{
        width: 100%;
        text-align: center;
    }
    .nav li ul li{
        border:1px solid black;
    }
    .nav li ul li:hover{
        background-color: #fff;
        cursor: pointer;
    }
    .nav a:hover{
        background-color: rgb(237,238,240);
        color: black;
    }
    .nav li ul li:hover{
        background-color: rgb(237,238,240);
    }
    .nav ul{
        display: none;
    }
</style>
<body>
    <ul class="nav">
        <li>
            <a href="#">微博</a>
            <ul >
                <li>消息</li>
                <li>关注</li>
                <li>@我</li>
            </ul>
        </li>
        <li>
            <a href="#">微博</a>
            <ul>
                <li>消息</li>
                <li>关注</li>
                <li>@我</li>
            </ul>
        </li>
        <li>
            <a href="#">微博</a>
            <ul>
                <li>消息</li>
                <li>关注</li>
                <li>@我</li>
            </ul>
        </li>
        <li>
            <a href="#">微博</a>
            <ul>
                <li>消息</li>
                <li>关注</li>
                <li>@我</li>
            </ul>
        </li>
    </ul>
</body>
<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='none';
        }
    }
</script>
</html>

猜你喜欢

转载自blog.csdn.net/weiyuyang250/article/details/120735779