原生js实现下拉菜单

2022-04-15 0 663

下拉菜单在实际生活中也挺常见的,它实现的js代码与tab选卡,手风琴几乎一样,在此不过多赘述。

我仿照苏宁易购官网写了一个下拉菜单,实现代码如下:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>下拉菜单</title>
    <style>
        body,
        ul {
            padding: 0;
            margin: 0;
        }
        body{
            background-color:#ccc;
        }
        li {
            list-style: none;
        }
        a{
            text-decoration: none;
        }
        a:hover{
            color: rgb(235, 98, 35);
        }
        .nav {
            float: right;
            margin-top: 10px;
            margin-right: 80px;
            display: flex;
            width: 270px;
            height: 100px;
        }
      
        .nav>li {
            width: 80px;
            margin: 5px;
            text-align: center;
        }
        .selected{
            width: 80px;
            background-color:#fff;
            color: rgb(235, 98, 35);
            border:1px solid rgb(196, 194, 194);
        }
        .nav>li div:nth-child(1){
            height: 30px;
            line-height: 30px; 
        }
        .nav>li div:nth-child(2){
            display: none;
            height: 160px;
            width: 80px;
            background-color: #fff;
            border:1px solid rgb(196, 194, 194);
            border-top:1px solid #fff;
            line-height: 70px;
        }
        .nav>li>div:nth-child(2) li{
            height: 40px;
            line-height: 40px;
        }
    </style>
</head>

<body>
    
    <ul class="nav">
        <li>
            <div><a herf="#">我的订单</a></div>
            <div>
            <ul>
                <li><a herf="#">待支付</a></li>
                <li><a herf="#">待发货</a></li>
                <li><a herf="#">待收货</a></li>
                <li><a herf="#">待评价</a></li>
            </ul>
            </div>
        </li>    
        <li>
            <div><a herf="#">我的易购</a></div>
            <div>
                    <ul>
                            <li><a herf="#">我的二手</a></li>
                            <li><a herf="#">我的关注</a></li>
                            <li><a herf="#">我的金融</a></li>
                            <li><a herf="#">苏宁会员</a></li>
                        </ul>
            </div>
        </li>
        <li>
            <div><a herf="#">我的主页</a></div>
            <div>
                    <ul>
                            <li><a herf="#">头像</a></li>
                            <li><a herf="#">昵称</a></li>
                            <li><a herf="#">签名</a></li>
                            <li><a herf="#">地址</a></li>
                        </ul>
            </div>
        </li>
    </ul>
    <script>
        var s=document.querySelectorAll(".nav li div:nth-child(1)");
        var d=document.querySelectorAll(".nav li div:nth-child(2)");
        for(var i=0;i<s.length;i++){
            s[i].index=i;
            s[i].onmouseover=function(){
                for(var j=0;j<s.length;j++){
                    s[j].className="";
                    d[j].style.display="none";
                }
                this.className="selected";
                d[this.index].style.display="block";
            }
        }
    </script>
   
</body>

</html>

效果图如下:

原生js实现下拉菜单

原生js实现下拉菜单

原生js实现下拉菜单

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript 原生js实现下拉菜单 https://www.niceym.com/24413.html