HW05-下拉菜单
使用JS实现下拉菜单效果
html代码如下
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{margin: 0; padding: 0; box-sizing: border-box;}
        html,body {width: 100%; height: 100%;}
        body{font-size: 62.5%;}
        .top {
            height: 50px;
            width: 100%;
            background-color: antiquewhite;
            display: flex;
            justify-content: center;
        }
        .head{
            height: 100%;
            width: 800px;
            /* background-color: aquamarine; */
            display: flex;
            justify-content: space-around;
            list-style: none;
            position: relative;
            right: 200px;
        }
        .head > .items{
            padding: 10px 40px;
            position: relative;
            cursor: pointer;
            font-size: 20px;
            text-align: center;
        }
        .head > .items:hover{
            background-color: #f2f2f1;
            color: #333;
            font-weight: 500;
        }
        .children{
            display: none;
            width: 100%;
            list-style: none;
            padding: 10px 10px;
            position: absolute;
            top: 50px;
            left: 0;
            background-color: antiquewhite;
        }
        .children > li{
            width: 100%;
            cursor: pointer;
            line-height: 40px;
        }
        .children > li:hover{
            background-color: #f2f2f1;
            color: #333;
            font-weight: 500;
        }
    </style>
</head>
<body>
    <div class="top">
        <ul class="head">
            <li class="items">首页</li>
            <li class="items">文章
                <ul class="children">
                    <li>发表文章</li>
                    <li>我的文章</li>
                    <li>我的收藏</li>
                </ul>
            </li>
            <li class="items">相册
                <ul class="children">
                    <li>我的相册</li>
                        <li>上传照片</li>
                </ul>
            </li>
            <li class="items">消息
                <ul class="children">
                    <li>我的留言</li>
                    <li>我的私信</li>
                </ul>
            </li>
        </ul>
    </div>
    <script>
        let _ils = document.getElementsByClassName('items');
        console.log(_ils);
        for(let i = 0; i<_ils.length; i++){
            _ils[i].onmouseenter = function(){
                let _children = _ils[i].getElementsByClassName('children');
                if(_children.length > 0){
                    let c = _children[0];
                        c.style.display = 'block';
                }
            }
        _ils[i].onmouseleave = function(){
            let _children = _ils[i].getElementsByClassName('children');
                if(_children.length > 0){
                    let c = _children[0];
                    c.style.display = "none";
                }
            }
        }
    </script>
</body>
</html>
效果截图

image.png