vue实现三级导航展示和隐藏

本文实例为大家分享了vue实现三级导航展示和隐藏的具体代码,供大家参考,具体内容如下

需求描述:

要实现侧边栏三级导航的显示和隐藏。点击其中一个一级导航,展示该一级导航的二级导航,再点击关闭该二级导航。一级导航的其他项,展开二级导航。关闭其他一级导航的二级导航。

效果如下:

代码:

<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: "项目信息",
          child: [
            {
              subTitle: "项目简介",
              esubTitle: "#projectIntroduction",
              threeChild: [
                { threeTitle: "三级导航" },
                { threeTitle: "三级导航" },
                { threeTitle: "三级导航" },
              ],
            },
            {
              subTitle: "样品信息",
              threeChild: [
                { threeTitle: "三级导航22" },
                { threeTitle: "三级导航22" },
                { threeTitle: "三级导航22" },
              ],
            },

            {
              subTitle: "样品信息",
              threeChild: [
                { threeTitle: "三级导航33" },
                { threeTitle: "三级导航33" },
                { threeTitle: "三级导航33" },
              ],
            },
          ],
        },
        {
          title: "项目信息2",
          child: [
            {
              subTitle: "项目简介22",
              threeChild: [
                { threeTitle: "三级导航44" },
                { threeTitle: "三级导44" },
                { threeTitle: "三级导航22" },
              ],
            },
            {
              subTitle: "样品信息22",
            },
          ],
        },
        {
          title: "项目信息3",
          eTitle: "#projectMessage",
          child: [
            {
              subTitle: "项目简介33",
              esubTitle: "#projectIntroduction",
            },
            {
              subTitle: "样品信息33",
              esubTitle: "#sampleInformation",
            },
          ],
        },
        {
          title: "项目信息2",
          child: [
            {
              subTitle: "项目简介22",
            },
            {
              subTitle: "样品信息22",
            },
          ],
        },
        {
          title: "项目信息3",
          child: [
            {
              subTitle: "项目简介33",
            },
            {
              subTitle: "样品信息33",
            },
          ],
        },
      ],

      currIndex: "", //当前索引
      spanIndex: [], //索引数组
      arrIndex: "", //用于判断是否做索引数组找到当前索引。-1为找不到,0找到了。

      currIndex2: "", //二级导航当前索引
      spanIndex2: [], //索引数组
      arrIndex2: "", //用于判断是否做索引数组找到当前索引。-1为找不到,0找到了。
    };
  },
  methods: {
    handleClick(index) {
      // 初始化三级导航,默认不显示。
      this.show3 =false;
      this.spanIndex2.splice(-1, 1);

      // 当前索引=index
      this.currIndex = index;
      console.log("当前索引index", index);
      // 判断当前索引是否在索引数组spanIndex中。arrIndex的值只有两种结果,-1未找到。0找到了。
      this.arrIndex = this.spanIndex.indexOf(index);
      console.log("arrIndex", this.arrIndex);

      if (this.arrIndex == 0) {
        //arrIndex ==0,找到索引了,在索引数组spanIndex删除该索引,隐藏二级导航。
        this.spanIndex.splice(this.arrIndex, 1);
        this.show2 = false;
      } else {
        // arrIndex ==-1,没有找到,splice(-1,1)从spanIndex数组结尾处删除1个值,并将当前索引添加到索引数组spanIndex,show2为true,展示二级导航,
        this.spanIndex.splice(this.arrIndex, 1);
        this.spanIndex.push(index);
        this.show2 = true;
      }

      console.log("索引数组", this.spanIndex);
    },

    secondClick(index) {
      console.log(index);
      // 当前索引=index
      this.currIndex2 = index;
      // 判断当前索引是否在索引数组spanIndex中。arrIndex的值只有两种结果,-1未找到。0找到了。
      this.arrIndex2 = this.spanIndex2.indexOf(index);
      console.log("arrIndex2", this.arrIndex2);

      if (this.arrIndex2 == 0) {
        //arrIndex ==0,找到索引了,在索引数组spanIndex删除该索引,隐藏二级导航。
        this.spanIndex2.splice(this.arrIndex2, 1);
        this.show3 = false;
      } else {
        // arrIndex ==-1,没有找到,splice(-1,1)从spanIndex数组结尾处删除1个值,并将当前索引添加到索引数组spanIndex,show2为true,展示二级导航,
        this.spanIndex2.splice(this.arrIndex2, 1);
        this.spanIndex2.push(index);
        this.show3 = true;
      }
       console.log("索引数组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>

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

(0)

相关推荐

  • 在vue中实现某一些路由页面隐藏导航栏的功能操作

    为了将导航栏显示在每一个页面中,可以将导航栏与<router-view>放在同一级显示,如下: <header> ... </header> <router-view></router-view> 但是,在某些时候,我们需要隐藏导航栏显示,比如登录界面,为了实现导航栏的隐藏,可以使用如下代码: <header v-show="$route.name!=='login'"> ... </header> &

  • vue实现动态显示与隐藏底部导航的方法分析

    本文实例讲述了vue实现动态显示与隐藏底部导航的方法.分享给大家供大家参考,具体如下: 在日常项目中,总有几个页面是要用到底部导航的,总有那么些个页面,是不需要底部导航的,这里列举一下页面底部导航的显示与隐藏的两种方式: 方式一: 1. 路由配置meta: {footShow: true, }, routes: [ { path: '/', name: 'home', redirect: '/home', // 默认路由添加class component: home, meta: { footS

  • vue 路由meta 设置导航隐藏与显示功能的示例代码

    vue 路由meta 设置title 导航隐藏,具体代码如下所示: router.js routes: [{ path: '/', name: 'HelloWorld', component: HelloWorld, meta: { title: "HelloWorld", 要现实的title show: true 设置导航隐藏显示 } }] App.vue <template> <div id="app"> <router-view&

  • vue实现三级导航展示和隐藏

    本文实例为大家分享了vue实现三级导航展示和隐藏的具体代码,供大家参考,具体内容如下 需求描述: 要实现侧边栏三级导航的显示和隐藏.点击其中一个一级导航,展示该一级导航的二级导航,再点击关闭该二级导航.一级导航的其他项,展开二级导航.关闭其他一级导航的二级导航. 效果如下: 代码: <template> <div id="app"> <img alt="Vue logo" src="./assets/logo.png"

  • vue实现动态控制el-table表格列的展示与隐藏

    本文实例为大家分享了vue动态控制el-table表格列的展示与隐藏的具体代码,供大家参考,具体内容如下 1.引入el-table组件,这里我直接用官网的示例代码 <template>     <div class="page">         <el-popover width="60" trigger="click">             <el-checkbox-group v-model=&

  • php+jQuery实现的三级导航栏下拉菜单显示效果

    本文实例讲述了php+jQuery实现的三级导航栏下拉菜单显示效果.分享给大家供大家参考,具体如下: 首先看看效果图: 1.数据配置文件 db.php <?php return array( array( 'one' => '关于我们', 'two' => array( array( 'three_tit' => '公司介绍', 'three_cont' => array( '企业概况', '组织架构', '发展历程', '企业文化', '服务理念' ) ), array(

  • layUI实现三级导航菜单效果

    本文实例为大家分享了layUI实现三级导航菜单展示的具体代码,供大家参考,具体内容如下 废话不多说,直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  • vue自定义底部导航栏Tabbar的实现代码

    如图所示,要完成类似的一个底部导航切换. 首先.我们需要分为5个大的VUE文件.可以根据自己的习惯来放在不同的位置. 我将5个主要的vue文件放在了5个不同的文件夹 然后,在components文件夹里新建Tabbar.vue/以及Item.vue文件 Item.vue文件如下 <template> <div class="itemWarp flex_mid" @click='changePage'> <span v-show='!bol'> <

  • vue实现中部导航栏布局功能

    接下来是中部导航栏.我们看到这里的头像动画,和中部导航栏定位都是跟鼠标滚动有关的.我们先将布局实现一下.这里是要求在页面上部分滚动范围内,导航栏一直在div的上部,随着鼠标的滚动而改变位置.到下部分滚动范围,导航栏就一直固定到页面的上部分. 这里需要注意两个地方 这里需要一个覆盖不了的区域,可以给人一种更好开关屏的感觉.而且中部导航栏下方区域的内容,在下滑的时候不能出现在这个区域. 一定要注意 尽可能的少进行DOM操作,这样是非常影响性能的 ! 监听鼠标滚动事件 private fixedFla

  • vue实现select下拉显示隐藏功能

    今日,怂怂就来说说,在项目中刚遇到这么一个功能需求: 描述:当下拉选择不同的属性选项,需展示对应的内容界面: select下拉菜单如下: 当下拉选择[表结构变更].即展示如下界面: 当下拉选择[接口变更].即展示如下界面: 代码实现 vue.js: //定义一个select下拉菜单 <el-form-item label="类型"> <el-select v-model="form.typeChanges"> <el-option la

  • vue实现二级导航栏效果

    本文实例为大家分享了vue实现二级导航栏效果展示的具体代码,供大家参考,具体内容如下 实现如下功能: 在.vue文件中,template中的内容如下: <template> <div id="app"> <nav class="sidebar"> <ul class="menu"> <li v-for="(navList,index) in navLists" :key=&

  • Vue.js 实现数据展示全部和收起功能

    如图所示,当我们获取到数据后每个栏都只显示5条,多出的部分隐藏,点击显示全部将数据都展现出来,如图所示 首先我们的数据类型是这样的, tableData: [ { "comment": "", "lscm": [ { "count": "1268", "id": 1, "namech": "OGC WMTS", "nameen"

  • Vue实现tab导航栏并支持左右滑动功能

    本文主要介绍:利用Vue实现tab导航栏,并且通过flex布局实现左右滑动,计算按钮的位置,当点击第一屏展示的最后一个且还有元素未展示时,自动滑动显示出未显示的元素. tab导航栏布局: <section class="theme-list"> <div class="fixed-nav" ref="fixednav"> <div class="fixed-nav-content"> <

随机推荐