2025-03-31

页面展示


代码演示

<!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:bisque;

        display:flex;

        justify-content:center;

}

.page-header{

        width:1200px;

        height:100%;/* background-color: orange; */

        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: #f2f2f1;

        color:#333;

        font-weight:500;

}

.children{

        display:none;

        position:absolute;

        top: 50px;

        left: 0;

        list-style:none;

        padding: 10px 20px;

        background-color:aquamarine;

}

.children > li{

        line-height: 50px;

}

.children > li:hover{

        background-color: #f2f2f1;

        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)

// 循环依次添加单击事件

    for(let i = 0; i < _lis.length; i++) {

// 依次添加单击事件 -> 鼠标进入事件

        _lis[i].onmouseenter = function() {

// 获取当前点击标签内部的<ul class="children"></ul>

            let _children = _lis[i].getElementsByClassName("children")

// 判断是否有子标签

            if(_children.length > 0) {

// 如果有,获取子标签

let c = _children[0]

// 判断显示子标签

c.style.display = "block"

}

}

// 依次添加一个鼠标离开事件

        _lis[i].onmouseleave = function(){

// 获取当前点击标签内部的<ul class="children"></ul>

            let _children = _lis[i].getElementsByClassName("children")

// 判断是否有子标签

            if(_children.length > 0) {

// 如果有,获取子标签

                let c = _children[0]

// 判断隐藏子标签

                c.style.display = "none"

            }

        }

    }

  </script>

</body>

</html>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 云计算Web下拉菜单作业: 1.作业展示 2.代码: <!DOCTYPE html> Documen...
    WwYiihung阅读 43评论 0 0
  • 前端 前端:任何与用户直接打交道的操作界面都可以称之为前端比如:电脑界面 手机界面 平板界面 后端:后端类似于幕后...
    浅笑_7cad阅读 4,355评论 3 1
  • ECMAscript 基础语法 变量 数据类型 运算符 数组 函数 对象 BOM 浏览器对象模型 window对象...
    浅笑_阅读 1,787评论 0 0
  • CSS 面试题汇总 1. 介绍下 BFC 及其应用 参考答案:参考答案:所谓 BFC,指的是一个独立的布局环境,...
    5cc9c8608284阅读 4,200评论 0 1
  • <!DOCTYPE html> Leon 序号 姓名 年龄 性别 ...
    小胖子_d7d8阅读 1,472评论 0 0

友情链接更多精彩内容