下拉菜单

作业分析

本次使用html与js样式编写效果如下


屏幕截图 2025-03-27 192443.png

屏幕截图 2025-03-27 192448.png

代码实现

使用html代码实现

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>shouye</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        html {
            font-size: 10px;
        }

        .bg {
            display: flex;
            justify-content: center;
            height: 5rem;
            width: 100%;
        }

        .content {
            width: 120rem;
            display: flex;

            line-height: 5rem;
            justify-content: space-evenly;
            list-style: none;
            font-size: 2rem;
            font-weight: 30;
        }

        .content .item {
            height: 100%;
            padding: 0 5rem;
            text-align: center;
            cursor: pointer;
            position: relative;
        }

        .content .item :hover {
            background-color: aqua;
            color: #000;
        }

        .chi {
            list-style: none;
            background-color: aqua;
            position: absolute;
            left: 0;
            top: 5rem;
            display: none;
        }

        .chi li {
            padding: 1rem 2rem;
        }

        .chi li :hover {
            background-color: aqua;
            color: #000;
        }
    </style>
</head>

<body>
    <div class="bg">
        <ul class="content">
            <li class="item">首页</li>
            <li class="item">文章
                <ul class="chi">
                    <li>我的文章</li>
                    <li>发表文章</li>
                </ul>
            </li>
            <li class="item">相册
                <ul class="chi">
                    <li>我的相册</li>
                    <li>上传照片</li>
                </ul>
            </li>
            <li class="item">消息
                <ul class="chi">
                    <li>我的私信</li>
                    <li>我的留言</li>
                </ul>
            </li>
        </ul>
    </div>
    <script>
        let lis = document.getElementsByClassName("item")
        for (let i = 0; i < lis.length; i++) {
            lis[i].onmouseenter = function () {
                let child = lis[i].getElementsByClassName("chi")
                if (child.length > 0) {
                    child[0].style.display = "block"
                }
            }

            lis[i].onmouseleave = function () {
                let child = lis[i].getElementsByClassName("chi")
                if (child.length > 0) {
                    child[0].style.display = "none"
                }
            }
        }
    </script>
</body>

</html>
#个人总结
要熟练运用使用js中函数的运用,以及对定义的熟练掌握,同时对标签的仔细检查
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容