Native js to implement drop-down menu

Native js to implement drop-down menu

Drop-down menus are also very common in real life. The js code used to implement them is almost the same as that of tab selection and accordion, so I will not go into details here.

I wrote a drop-down menu based on Suning.com's official website. The implementation code is as follows:

<!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>Drop-down menu</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="#">My Order</a></div>
            <div>
            <ul>
                <li><a herf="#">Awaiting payment</a></li>
                <li><a herf="#">Waiting for shipment</a></li>
                <li><a herf="#">Waiting for delivery</a></li>
                <li><a herf="#">Awaiting evaluation</a></li>
            </ul>
            </div>
        </li>    
        <li>
            <div><a herf="#">My Yigoo</a></div>
            <div>
                    <ul>
                            <li><a herf="#">My Second-hand</a></li>
                            <li><a herf="#">My Follow</a></li>
                            <li><a herf="#">My Finance</a></li>
                            <li><a herf="#">Suning Member</a></li>
                        </ul>
            </div>
        </li>
        <li>
            <div><a herf="#">My Homepage</a></div>
            <div>
                    <ul>
                            <li><a herf="#">Avatar</a></li>
                            <li><a herf="#">Nickname</a></li>
                            <li><a herf="#">Signature</a></li>
                            <li><a herf="#">Address</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>

The effect diagram is as follows:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript drop-down menu implementation code
  • css+js drop-down menu
  • js dynamically sets the default selected item in the select drop-down menu
  • JS implementation code for the three-level drop-down menu
  • A js implementation code for a date drop-down menu
  • JS real multi-level linkage drop-down menu class, easily realize the linkage menu of provinces, cities and districts!
  • Js click pop-up drop-down menu effect example
  • Three ways to implement the secondary drop-down menu in the navigation menu in JS
  • JavaScript to show and hide the drop-down menu
  • js to achieve the effect code of cascading drop-down menu of provinces and cities across the country

<<:  MySQL 8.0.20 compressed version installation tutorial with pictures and text

>>:  Configure selenium environment based on linux and implement operation

Recommend

Use of Vue filters and custom instructions

Table of contents Filters 01.What is 02. How to d...

A Brief Analysis of MySQL PHP Syntax

Let's first look at the basic syntax of the c...

Detailed tutorial on building an ETCD cluster for Docker microservices

Table of contents Features of etcd There are thre...

Implementation of webpack code fragmentation

Table of contents background CommonsChunkPlugin s...

HTML embed tag usage and attributes detailed explanation

1. Basic grammar Copy code The code is as follows...

Docker solves the problem that the terminal cannot input Chinese

Preface: One day, I built a MySQL service in Dock...

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

How to change the CentOS server time to Beijing time

1. I purchased a VPS and CentOS system, and found...

Pure CSS to implement iOS style open and close selection box function

1 Effect Demo address: https://www.albertyy.com/2...

Detailed explanation of the use of filter properties in CSS3

Recently, when I was modifying the intranet porta...

Docker builds python Flask+ nginx+uwsgi container

Install Nginx First pull the centos image docker ...

React tsx generates random verification code

React tsx generates a random verification code fo...