[VUE小课堂]仿制淘宝网顶部导航栏组件

JS部分

const props = defineProps({
  nav_rightList: {
    type: Array
  },
})

// 划过事件
let showSubMenu = (id) => {
  const item = props.nav_rightList.find((item) => item.id === id);
  item.showSubMenu = true;
}
let hideSubMenu = (id) => {
  const item = props.nav_rightList.find((item) => item.id === id);
  item.showSubMenu = false;
}
// 点击事件,根据业务需求跳转
let toMypage = (e) => {
  emit('toMypage', e)
}
let toMypageChild = (e) => {
  emit('toMypageChild', e)
}

在这个例子中,我们使用v-for指令来循环遍历菜单项,并使用@mouseover@mouseleave事件处理程序来显示和隐藏菜单项的子菜单。在方法中,我们查找与给定ID相对应的菜单项,并将其showSubMenu属性设置为true或false,从而显示或隐藏子菜单。

<template>
  <div>
    <div class="nav_wrap same">
      <div class="nav_left">
        <div
          class="left_item"
          v-for="item in nav_leftList"
          :key="item.id"
        >{{ item.name }} </div>
      </div>
      <div class="nav_right">
        <ul class="right_item">
          <li
            class="top1_txt"
            v-for="item in props.nav_rightList"
            :key="item.id"
            @mouseenter="showSubMenu(item.id)"
            @mouseleave="hideSubMenu(item.id)"
            @click="toMypage(item)"
          >
            {{ item.name }}
            <ul
              class="sub-menu"
              v-if="item.showSubMenu === true"
            >
              <li
                @click.stop="toMypageChild(item)"
                v-for="subItem in item.sub"
                :key="subItem.id"
              >{{ subItem.name }}</li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

CSS方面,我们使用display: none将子菜单隐藏,并在鼠标悬停在菜单项上时,将其显示为display: block

请注意,此示例中使用了一些基本样式。您可以根据需要进行调整。

.left_item {
  cursor: pointer;
  padding: 0 10px;
}

.left_item:hover {
  color: rgb(255, 123, 0);
}

.nav_right {
  display: flex;
  justify-content: flex-end;
  margin-left: 25%;

  .sub-menu {
    display: none;
    position: absolute;
    top: 40px;
    padding: 0;
    margin: 0;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    z-index: 7;
    animation: scale-up-ver-top .4s cubic-bezier(.39, .575, .565, 1.000) both
  }

  li:hover .sub-menu {
    transition: all 0.5s;
    display: block;
  }

  .right_item {
    display: flex;

    li {
      cursor: pointer;
      padding: 0 10px;
    }

    .top1_txt:hover {
      color: rgb(255, 123, 0);
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名BUG制造者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值