Html+CSS+JS实现侧边下拉导航栏(web前端开发基础)

该代码源自我昨天在B站看的一个视频课(av27073032),造福群众,不喜勿喷。

HTML源代码(建议用Chrome浏览器打开,兼容性强,不易出现乱码现象):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Keywords" content="">
    <meta name="Description" content="">
    <title>侧边下拉导航栏</title>
</head>
<style type="text/css">
    *{
        padding: 0;
        margin: 0;
    }
    html,body{
        height: 100%;
    }
    .wrap{
        width: 260px;
        height: 100%;
        background-color: #363a45;
    }
    .header{
        width: 100%;
        height: 65px;
        background-color: #44495a;
        font-size: 24px;
        color: #fff;
        text-align: center;
        line-height: 65px;
    }
    .nav{
        width:250px;
        margin: 10px 5px 0;
    }
    .list{
        margin-bottom: 5px;
    }
    .list h2{
        position: relative;
        padding: 15px 0;
        background-color: #3889d4;
        text-align: center;
        font-size: 16px;
        color: #fff;
        transition: .5s;
    }
    .list h2.on{
        background-color: #393c4a;
    }
    .list i{
        position: absolute;
        right: 10px;
        top: 16px;
        border: 8px solid transparent;
        border-left-color: #fff;/*triangle*/
        transform:rotate(0deg);
        transform-origin: 1px 8px;
        transition: .5s;
    }
    .list i.on{
        transform:rotate(90deg);
    }
    .hide{
        overflow: hidden;
        height: 0;
        transition: .5s;
    }
    .hide h5{
        padding: 10px 0;
        background-color: #282c3a;
        text-align: center;
        color:#fff;
        border-bottom:#42495d;
    }
</style>
<body>
    <div class="wrap">
        <div class="header">国内各个景点</div>
        <div class="nav">
            <ul>
                <li class="list">
                    <h2><i></i>北京景点</h2>
                    <div class="hide">
                        <h5>故宫</h5>
                        <h5>圆明园</h5>
                        <h5>天坛</h5>
                        <h5>长城</h5>
                        <h5>天安门</h5>
                        <h5>雍和宫</h5>
                    </div>
                </li>
                <li class="list">
                    <h2><i></i>南京景点</h2>
                    <div class="hide">
                        <h5>夫子庙</h5>
                        <h5>南京总统府</h5>
                        <h5>明孝陵</h5>
                        <h5>长江大桥</h5>
                        <h5>南京博物院</h5>
                        <h5>雨花台</h5>
                    </div>
                </li>
                <li class="list">
                    <h2><i></i>西安景点</h2>
                    <div class="hide">
                        <h5>钟楼</h5>
                        <h5>秦皇陵</h5>
                        <h5>华清池</h5>
                        <h5>大唐芙蓉园</h5>
                        <h5>秦岭野生动物园</h5>
                        <h5>樱花广场</h5>
                    </div>
                </li>
                <li class="list">
                    <h2><i></i>重庆景点</h2>
                    <div class="hide">
                        <h5>洪崖洞</h5>
                        <h5>丰都鬼城</h5>
                        <h5>金刀峡</h5>
                        <h5>重庆仙女山</h5>
                        <h5>白公馆</h5>
                        <h5>三峡博物馆</h5>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <script>
        (function () {
            var oList = document.querySelectorAll('.list h2'),
                oHide = document.querySelectorAll('.hide'),
                oIcon = document.querySelectorAll('.list i'),
                lastIndex = 0;
            for(var i=0;i<oList.length;i++){
                oList[i].index = i;//自定义属性
                oList[i].isClick = false;
                oList[i].initHeight = oHide[i].clientHeight;
                oHide[i].style.height = '0';
                oList[i].onclick = function () {
                    if(this.isClick){
                        oHide[this.index].style.height = '0';
                        oIcon[this.index].className = '';
                        oList[this.index].className = '';
                        oList[this.index].isClick = false;
                    }
                    else{
                        oHide[lastIndex].style.height = '0';
                        oIcon[lastIndex].className = '';
                        oList[lastIndex].className = '';
                        oHide[this.index].style.height = '220px';
                        oIcon[this.index].className = 'on';
                        oList[this.index].className = 'on';
                        oList[lastIndex].isClick = false;
                        oList[this.index].isClick = true;
                        lastIndex = this.index;
                    }
                }
            }
        })();
    </script>
</body>
</html>
  • 44
    点赞
  • 262
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,以下是一个简单的用 HTMLCSS 写的小米商城导航栏下拉框的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>小米商城导航栏下拉框</title> <style> /* 导航栏样式 */ .nav { background-color: #f7f7f7; padding: 10px; } /* 下拉框样式 */ .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #fff; min-width: 160px; z-index: 1; } .dropdown:hover .dropdown-content { display: block; } /* 下拉框链接样式 */ .dropdown-content a { color: #333; padding: 10px; text-decoration: none; display: block; } .dropdown-content a:hover { background-color: #f7f7f7; } </style> </head> <body> <!-- 导航栏 --> <div class="nav"> <!-- 下拉框 --> <div class="dropdown"> <a href="#">手机</a> <div class="dropdown-content"> <a href="#">小米手机</a> <a href="#">Redmi手机</a> <a href="#">黑鲨手机</a> </div> </div> <div class="dropdown"> <a href="#">电视</a> <div class="dropdown-content"> <a href="#">小米电视</a> <a href="#">Redmi电视</a> </div> </div> <div class="dropdown"> <a href="#">笔记本</a> <div class="dropdown-content"> <a href="#">小米笔记本</a> <a href="#">Redmi笔记本</a> <a href="#">游戏本</a> </div> </div> <!-- 其他导航链接 --> <a href="#">智能硬件</a> <a href="#">服务</a> <a href="#">社区</a> </div> </body> </html> ``` 上面的代码中,我们使用了 `div` 元素来创建导航栏下拉框,使用了 CSS 来设置它们的样式。在下拉框部分,我们使用了 `position` 属性来设置下拉框的位置,使用了 `display` 属性来设置下拉框的显示和隐藏,使用了 `z-index` 属性来设置下拉框的层级。在下拉框链接部分,我们使用了 `a` 元素来创建链接,使用了 CSS 来设置它们的样式。 请注意,这只是一个示例代码,实际的小米商城导航栏下拉框可能会更加复杂,需要根据具体的需求和设计进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值