vue pc官网顶部导航栏组件

官网顶部导航分为一级导航和二级导航

导航的样子

文件的层级

router 文件层级

header 组件代码

<h1 class="logo-wrap">
          <router-link to="/">
            <img class="logo" :src="$config.company.logo" alt="" />
            <img class="sub-logo" :src="$config.company.subLogo" alt="" />
          </router-link>
        </h1>
        <!-- 导航 -->
        <div class="menu-wrap">
          <ul>
            <!-- 一级 -->
            <li
                v-for="(item, index) in menus"
                @click="handle(index)"
                :class="{ active: index == menuIndex }"
                class="menu-item"
                v-if="$isNull(item.meta.isMenuHide)">
              <a href="javascript:;" class="a" @click="menuClick(item.path)">{{
                item.meta.title
              }}</a>
              <!-- 二级 -->
              <span class="child-menu-group" v-if="!item.meta.isChildMenuHide">
                <span
                      class="chil-item"
                      v-for="(childItem, childIndex) in item.children"
                      v-if="$isNull(childItem.meta.isMenuHide)">
                  <a
                     href="javascript:;"
                     class="child-a"
                     @click="menuClick(childItem.path)">{{ childItem.meta.title }}</a>
                </span>
              </span>
            </li>
          </ul>
        </div>

js 、css

 .logo-wrap {
    float: left;

    .logo,
    .sub-logo {}

    .sub-logo {
      display: inline-block;
      margin-left: 5px;
      padding-left: 5px;
      border-left: 1px #ffffff40 solid;
    }
  }  

.menu-wrap {
    float: right;

    .menu-item.active {
      &:before {
        position: absolute;
        content: "";
        bottom: 0;
        left: 50%;
        width: 30px;
        transform: translate3d(-50%, 0, 0);
        border-bottom: 2px #ffffff solid;
      }
    }

    li {
      position: relative;
      display: inline-block;

      &.active {}

      .a {
        display: block;
        padding: 15px 20px;
        color: #fff;
        font-size: 16px;
      }

      .child-menu-group {
        display: none;
        position: absolute;
        left: -30px;
        min-width: 160px;
        padding: 5px 0;
        background-color: #ffffff;
        border-radius: 6px;
        text-align: center;
        box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);

        &:before {
          position: absolute;
          content: "";
          top: -6px;
          left: 50%;
          margin-left: -6px;
          width: 0;
          height: 0;
          border-left: 4px solid transparent;
          border-right: 4px solid transparent;
          border-bottom: 8px solid #ffffff;
        }

        .chil-item {
          position: relative;
          display: block;

          &::after {
            position: absolute;
            z-index: 1;
            top: 0;
            left: 0;
            content: "";
            height: 100%;
            width: 0;
            background-image: @background-image;
          }

          @keyframes childItem {
            0% {
              width: 0;
            }

            100% {
              width: 100%;
            }
          }

          &:hover {
            &::after {
              animation: childItem 0.2s ease-out;
              width: 100%;
            }

            a {
              color: #ffffff;
            }
          }
        }

        .child-a {
          display: block;
          position: relative;
          z-index: 2;
          padding: 10px 0;
        }
      }

      &:hover {
        .child-menu-group {
          display: block;
        }
      }
    }
  }
 data() {
    return {
      menus: [],
      menuIndex: 0,
      isMenuDrawer: false,
      isMenuFixed: false
    };
  },
  created() {
    this.menus = this.$router.options.routes[1].children;
    console.log( this.menus)
    this.findRouter(this.$route.path);
    this.resetPath(this.$route.path);
  },
  mounted() {
    this.onScroll();
  },
  watch: {
    //监听路由变化
    $route(to, from) {
      this.isMenuFixed = false;
      this.resetPath(to.path);
      this.findRouter(to.path);
    }
  },
  methods: {
    findRouter(path) {
      var i = path.indexOf("/", 1);
      var pathTo = "";
      if(i == -1) {
        pathTo = path;
      } else {
        pathTo = path.substring(0, i);
      }
      var list = this.menus;
      var index = list.findIndex((item, index) => {
        return pathTo == item.path;
      });
      this.menuIndex = index;
      document.title = this.menus[this.menuIndex].meta.title
    },
   
    // 二级菜单移动端
    menuSwitch() {
      this.isMenuDrawer = true;
    },
    // 点击菜单
    menuClick(path) {
      this.isMenuDrawer = false;
      this.$router.push(path);
    },
    // 导航切换
    handle(index) {
      this.isMenuDrawer = false;
      this.menuIndex = index;
      document.title = this.menus[this.menuIndex].meta.title
    },
    //获取页面路由,导航添加active
    resetPath(path) {
      var _menus = this.menus;
      for(var i in _menus) {
        if(_menus[i].path == path) {
          this.menuIndex = i;
          // 设置标题
          document.title = _menus[i].meta.title;
        }
      }
    }
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据引用\[1\]和引用\[2\],可以使用Vue3和Element UI来实现左侧导航栏的动效。首先,在Vue组件中使用Element UI的navbar组件来创建左侧菜单栏导航。然后,可以使用鼠标移入移出事件来触发下拉动画效果。通过设置CSS样式,可以让下拉菜单铺满全屏,使其宽度为100%。这样就可以实现左侧导航栏的动效。引用\[1\]中提供了具体的代码示例,你可以参考其中的.nav和.dropdown-content的CSS样式设置来实现动效。同时,引用\[3\]中提供了一个解决刷新页面导致数据重置的问题的方法,可以在Vue组件的mounted()生命周期钩子中添加一个事件监听器,当页面刷新时,将页面滚动位置重置为顶部,以保持导航高亮的正确性。 #### 引用[.reference_title] - *1* [vue导航栏下拉菜单(附带展开收缩动画)](https://blog.csdn.net/wgh4318/article/details/126426602)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [记一次vue--NavMenu--左侧导航栏动画效果折腾记](https://blog.csdn.net/ww_5211314/article/details/103425950)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Vue实现导航栏绑定内容锚点+滚动动画+vue-router](https://blog.csdn.net/Z2564663790/article/details/129769040)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值