WEB第五次作业

作业分析

下拉菜单 2025-03-23 220537.png

代码示例

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>下拉菜单</title>
    <style>

        /* 公共样式 */
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        html,body{
            width: 100%;
            height: 100%;
        }
        body{
            font-size: 62.5%;
        }

        /* 自定义样式 */
        .nav{
            width: 100%;
            height: 50px;
            background-color: #bea594;
            display: flex;
            justify-content: center;
        }
        .page-header{
            width: 1200px;
            height: 100%;
            background-color: #d7ad9b;
            display: flex;
            list-style: none;
        }
        .page-header > .item{
            padding: 10px 50px;
            font-size: 18px;
            text-align: center;
            cursor: pointer;
            position: relative;
        }
        .page-header > .item:hover{
            background-color: #e5dcd1;
            color: #333;
            font-weight: 500;
        }
        .children{
            display: none;
            position: absolute;
            top: 50px;
            left: 0;
            list-style: none;
            padding: 10px 20px;
            background-color: #e6d7d7;
        }
        .children > li{
            line-height: 50px;
        }
        .children > li{
            background-color: #c4cdd0;
            color: #333;
        }
    </style>
</head>
<body>
    <div class="nav">
        <ul class="page-header">
            <li class="item">首页</li>
            <li class="item">文章
                <ul class="children">
                    <li>发表文章</li>
                    <li>我的文章</li>
                    <li>关注的文章</li>
                    <li>喜欢的文章</li>
                </ul>
            </li>
            <li class="item">相册
                <ul class="children">
                    <li>我的相册</li>
                    <li>上传照片</li>
                </ul>
            </li>
            <li class="item">消息
                <ul class="children">
                    <li>我的私信</li>
                    <li>我的留言</li>
                </ul>
            </li>
        </ul>
    </div>

    <!-- JS代码写在网页的最后面 -->
    <script>
        //获取所有的一级菜单;<li class="item"></li>
        let  _lis = document.getElementsByClassName("item")
        console.log(_lis)

        //封装函数
        function toggleMune(child,is_show){
            //获取当前点击标签内部的<ul class="children"></ul>
            let _children = child.getElementsByClassName("children")
            //判断是否有子标签
            if(_children.length > 0 ){
                //如果有,获取子标签
                let c = _children[0]
                //判断显示子标签
                c.style.display = is_show
            }
        }

        //循环依次添加单击事件
        for(let i = 0;i < _lis.length;i ++){
            //console.log(_lis[i],"---")
            //依次添加单击事件 -> 鼠标进入事件
            _lis[i].onmouseenter = function(){
                //显示子菜单
                toggleMune(_lis[i],"block")
            }

            //依次添加一个鼠标离开事件
            _lis[i].onmouseleave = function(){
                //隐藏子菜单
                toggleMune(_lis[i],"none")
            }
        }
    </script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容