Vue implements three-level navigation display and hiding

Vue implements three-level navigation display and hiding

This article example shares the specific code of Vue to realize the display and hiding of three-level navigation for your reference. The specific content is as follows

Requirement description:

To realize the display and hiding of the three-level navigation of the sidebar. Click on one of the primary navigations to display the secondary navigation of that primary navigation, then click to close that secondary navigation. For other items in the primary navigation, expand the secondary navigation. Closes the secondary navigation of other primary navigations.

The effect is as follows:

Code:

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <HelloWorld msg="Welcome to Your Vue.js App" />
    <div class="first" v-for="(item, key) in navLists" :key="key">
      <li>
        <span @click="handleClick(key)"> {{ item.title }}</span>
        <div
          v-for="(item2, key2) in item.child"
          :key="key2"
          class="second"
          v-show="show2 && currIndex == key"
        >
          <p @click="secondClick(key2)">{{ item2.subTitle }}</p>
          <div
            v-for="(item3, key3) in item2.threeChild"
            :key="key3"
            class="three"
           v-show="show3 && currIndex2 == key2"
          >
            {{ item3.threeTitle }}
          </div>
        </div>
      </li>
    </div>
  </div>
</template>
 
<script>
import HelloWorld from "./components/HelloWorld.vue";
 
export default {
  name: "App",
  components:
    HelloWorld,
  },
  data() {
    return {
      i: 0,
 
      show3: false,
      show2: false,
      navLists: [
        {
          title: "Project Information",
          child: [
            {
              subTitle: "Project Introduction",
              esubTitle: "#projectIntroduction",
              threeChild: [
                { threeTitle: "Three-level navigation" },
                { threeTitle: "Three-level navigation" },
                { threeTitle: "Three-level navigation" },
              ],
            },
            {
              subTitle: "Sample Information",
              threeChild: [
                { threeTitle: "Third level navigation 22" },
                { threeTitle: "Third level navigation 22" },
                { threeTitle: "Third level navigation 22" },
              ],
            },
 
            {
              subTitle: "Sample Information",
              threeChild: [
                { threeTitle: "Third level navigation 33" },
                { threeTitle: "Third level navigation 33" },
                { threeTitle: "Third level navigation 33" },
              ],
            },
          ],
        },
        {
          title: "Project Information 2",
          child: [
            {
              subTitle: "Project Introduction 22",
              threeChild: [
                { threeTitle: "Three-level navigation 44" },
                { threeTitle: "Level 3 Guide 44" },
                { threeTitle: "Third level navigation 22" },
              ],
            },
            {
              subTitle: "Sample Information 22",
            },
          ],
        },
        {
          title: "Project Information 3",
          eTitle: "#projectMessage",
          child: [
            {
              subTitle: "Project Introduction 33",
              esubTitle: "#projectIntroduction",
            },
            {
              subTitle: "Sample Information 33",
              esubTitle: "#sampleInformation",
            },
          ],
        },
        {
          title: "Project Information 2",
          child: [
            {
              subTitle: "Project Introduction 22",
            },
            {
              subTitle: "Sample Information 22",
            },
          ],
        },
        {
          title: "Project Information 3",
          child: [
            {
              subTitle: "Project Introduction 33",
            },
            {
              subTitle: "Sample Information 33",
            },
          ],
        },
      ],
 
      currIndex: "", //Current index spanIndex: [], //Index array arrIndex: "", //Used to determine whether to find the current index by indexing the array. -1 means not found, 0 means found.
 
      currIndex2: "", //Current index of secondary navigation spanIndex2: [], //Index array arrIndex2: "", //Used to determine whether to find the current index by indexing the array. -1 means not found, 0 means found.
    };
  },
  methods: {
    handleClick(index) {
      // Initialize the third-level navigation, which is not displayed by default.
      this.show3 =false;
      this.spanIndex2.splice(-1, 1);
 
      // Current index = index
      this.currIndex = index;
      console.log("current index", index);
      // Determine whether the current index is in the index array spanIndex. There are only two possible values ​​for arrIndex: -1 not found. 0 found.
      this.arrIndex = this.spanIndex.indexOf(index);
      console.log("arrIndex", this.arrIndex);
 
      if (this.arrIndex == 0) {
        //arrIndex == 0, the index is found, delete the index in the index array spanIndex, and hide the secondary navigation.
        this.spanIndex.splice(this.arrIndex, 1);
        this.show2 = false;
      } else {
        // arrIndex ==-1, not found, splice(-1,1) deletes 1 value from the end of the spanIndex array and adds the current index to the index array spanIndex, show2 is true, showing the secondary navigation,
        this.spanIndex.splice(this.arrIndex, 1);
        this.spanIndex.push(index);
        this.show2 = true;
      }
      
      console.log("index array", this.spanIndex);
    },
 
    secondClick(index) {
      console.log(index);
      // Current index = index
      this.currIndex2 = index;
      // Determine whether the current index is in the index array spanIndex. There are only two possible values ​​for arrIndex: -1 not found. 0 found.
      this.arrIndex2 = this.spanIndex2.indexOf(index);
      console.log("arrIndex2", this.arrIndex2);
 
      if (this.arrIndex2 == 0) {
        //arrIndex == 0, the index is found, delete the index in the index array spanIndex, and hide the secondary navigation.
        this.spanIndex2.splice(this.arrIndex2, 1);
        this.show3 = false;
      } else {
        // arrIndex ==-1, not found, splice(-1,1) deletes 1 value from the end of the spanIndex array and adds the current index to the index array spanIndex, show2 is true, showing the secondary navigation,
        this.spanIndex2.splice(this.arrIndex2, 1);
        this.spanIndex2.push(index);
        this.show3 = true;
      }
       console.log("index array 2", this.spanIndex2);
    },
  },
};
</script>
 
<style>
p {
  padding: 5px 0;
  margin-block-start: 0;
  margin-block-end: 0;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.first {
  width: 200px;
  font-size: 24px;
  font-weight: bold;
  /* height: 60px; */
  /* background:red; */
}
.first:hover {
  cursor: pointer;
 
  /* color:red; */
}
.second {
  font-size: 18px;
  font-weight: normal;
  background: #eee;
  margin-left: 50px;
}
.three {
  background: yellow;
  margin-left: 20px;
  font-size: 14px;
}
</style>

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:
  • Implement the function of hiding the navigation bar on some routing pages in Vue
  • Vue routing meta set navigation hide and show function sample code
  • Analysis of Vue's method of dynamically displaying and hiding bottom navigation

<<:  When Navicat connects to MySQL, it reports 10060, 1045 errors and the location of my.ini

>>:  Python Flask WeChat applet login process and login api implementation code

Recommend

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

SQL implementation of LeetCode (177. Nth highest salary)

[LeetCode] 177.Nth Highest Salary Write a SQL que...

A brief discussion on how to customize the host file in Docker

Table of contents 1. Command 2. docker-compose.ym...

Linux uses join -a1 to merge two files

To merge the following two files, merge them toge...

Vue 2.0 Basics in Detail

Table of contents 1. Features 2. Examples 3. Opti...

js native carousel plug-in production

This article shares the specific code for the js ...

Summary of commonly used escape characters in HTML

The commonly used escape characters in HTML are s...

Analysis of log files in the tomcat logs directory (summary)

Each time tomcat is started, the following log fi...

Vue3.0 routing automatic import method example

1. Prerequisites We use the require.context metho...

Windows platform configuration 5.7 version + MySQL database service

Includes the process of initializing the root use...

JavaScript design pattern learning proxy pattern

Table of contents Overview Implementation Protect...

Layim in javascript to find friends and groups

Currently, layui officials have not provided the ...

How to upgrade https under Nginx

Purchase Certificate You can purchase it from Ali...