JavaScript to implement retractable secondary menu

JavaScript to implement retractable secondary menu

The specific code for implementing the retractable secondary menu in JavaScript is for your reference. The specific content is as follows

Retractable secondary menu:

Case description: The effect diagram is as follows. Only one can be opened at a time. After opening, the + sign changes to -.

HTML Code

<!--Pull list-->
<style>
 
    li {
        /*Cancel the li dot*/
        list-style: none;
    }
 
    li span {
        /*Set the background image for the span tag (small plus and minus signs at the beginning, no-repeat, initial position 0 0)*/
        background: url(add.png) no-repeat left center;
        padding-left: 20px;
    }
 
    /*Starting style*/
    li ul{
        height: 0;
        /*Overflow hidden*/
        overflow: hidden;
        /*Add transition effect*/
        transition: all 0.5s;
    }
    /*Expand style*/
    .open{
        background-image: url(minus.png);
    }
    .open+ul{
        height: 70px;
    }
 
 
</style>
 
 
<ul class="tree">
    <li><span class="open">Attendance Management</span>
        <ul>
            <li>Daily Attendance</li>
            <li>Leave Application</li>
            <li>Overtime business trip</li>
        </ul>
    </li>
    <li><span>Information Center</span>
        <ul>
            <li>Daily Attendance</li>
            <li>Leave Application</li>
            <li>Overtime business trip</li>
        </ul>
    </li>
    <li><span>Collaborative Office</span>
        <ul>
            <li>Daily Attendance</li>
            <li>Leave Application</li>
            <li>Overtime business trip</li>
        </ul>
    </li>
</ul>
 
<script src="mJS.js"></script>

JavaScript code

// Pull-out list // Implementation ideas:
 
var spans = document.querySelectorAll(".tree span");
 
for (var i = 0; i < spans.length; i++) {
    spans[i].onclick = function () {
        // console.log(this);
        // Find the element span to be modified // Modify, delete or add class -> open
        // Check if there is an open on itself, if so, delete it!
        // this -> points to the element that triggered the event if (this.className == "open") {
            // If there is, delete it!
            this.className = "";
        } else {
            // If not, add it and delete the others that are already opened!
            //Find the open (li of the open attribute)
            var openSpan = document.querySelector(".tree.open")
            if (openSpan !== null) {
                // If the collection is not empty, delete their open attribute openSpan.className = "";
            }
            // Set the open attribute for yourself this.className = "open";
        }
 
 
    }
}

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
  • Universal secondary menu code (css+javascript)
  • A simple js tree 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 to achieve simple folding and expanding menu method
  • JavaScript realizes the three-level linkage drop-down box menu of provinces, cities and districts
  • Js click pop-up drop-down menu effect example
  • JavaScript to show and hide the drop-down menu

<<:  In-depth understanding of the use of r2dbc in MySQL

>>:  How to set background blur with CSS

Recommend

Zen Coding Easy and fast HTML writing

Zen Coding It is a text editor plugin. In a text ...

Detailed explanation of key uniqueness of v-for in Vue

Table of contents 1. DOM Diff 2. Add key attribut...

W3C Tutorial (6): W3C CSS Activities

A style sheet describes how a document should be ...

React entry-level detailed notes

Table of contents 1. Basic understanding of React...

A method of hiding processes under Linux and the pitfalls encountered

Preface 1. The tools used in this article can be ...

Implementation of TypeScript in React project

Table of contents 1. Introduction 2. Usage Statel...

W3C Tutorial (11): W3C DOM Activities

The Document Object Model (DOM) is a platform, a ...

Automated front-end deployment based on Docker, Nginx and Jenkins

Table of contents Preliminary preparation Deploym...

How to restore a database and a table from a MySQL full database backup

In the official MySQL dump tool, how can I restor...

Two ways to open and close the mysql service

Method 1: Use cmd command First, open our DOS win...

Using iframe techniques to obtain visitor QQ implementation ideas and sample code

Today at work, a friend I added temporarily asked ...

Implementation of code optimization for Vue2.x project performance optimization

Table of contents 1 Use of v-if and v-show 2. Dif...

How to redirect to other pages in html page within two seconds

Copy code The code is as follows: <!DOCTYPE ht...

Detailed explanation of Axios asynchronous communication in Vue

1. First, we create a .json file for interactive ...