vue项目实现 导航栏 的方式

本文介绍了两种在Vue中创建头部导航的方法:一是使用`router-link`实现页面间的跳转,并通过`router-link-active`控制选中状态;二是通过点击事件改变变量实现效果。第一种方法简洁高效,第二种则相对繁琐。建议使用`router-link`进行组件间导航。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现效果:

方式一:利用router-link实现

首先:新建一个组件,将头部导航栏提取出来,使多个组件可以公用一个头部。

代码如下: 

<template>
  <div>
    <div class="xl-flex" style="margin-left: 560px">
      <router-link class="title_change" to="/">
        <div class="titleIcon1" style="width: 24px; height: 24px"></div>
        <span class="titleSpan">首页</span>
      </router-link>
      <router-link class="title_change" to="/project-detail-all">
        <div class="titleIcon6" style="width: 24px; height: 24px"></div>
        <span class="titleSpan">项目</span>
      </router-link>
      <router-link class="title_change" to="/evaluate">
        <div class="titleIcon6" style="width: 24px; height: 24px"></div>
        <span class="titleSpan">后评价模块</span>
      </router-link>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
    name: "title-index",
    data() {
        return {
        };
    }
};
</script>

<style scoped lang="less">
.title_change {
  display: flex;
  align-items: center;
  padding: 4px 16px;
  box-sizing: border-box;
  cursor: pointer;
  margin-right: 76px;
  text-decoration: none;
  .titleSpan {
    font-size: 18px;
    font-family: PingFang SC;
    font-weight: 500;
    line-height: 0px;
    color: #00d8ff;
  }
  .titleIcon1 {
    background: url("@/assets/images/icon/title-home-icon.png") center no-repeat;
    background-size: 100%;
  }
  .titleIcon2 {
    background: url("@/assets/images/icon/title-comprehensiveReview-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon3 {
    background: url("@/assets/images/icon/title-powerAccess-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon4 {
    background: url("@/assets/images/icon/title-plan-icon.png") center no-repeat;
    background-size: 100%;
  }
  .titleIcon5 {
    background: url("@/assets/images/icon/title-investManege-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon6 {
    background: url("@/assets/images/icon/title-allPlan-icon.png") center
      no-repeat;
    background-size: 100%;
  }
}
.router-link-active { // 控制点击router-link之后的样式
  
  background: url("../../assets/images/icon/fiveWord-chosed-background.png")
    center no-repeat;
  background-size: 100% 100%;
  .titleSpan {
    color: #ff942c;
  }
  .titleIcon1 {
    background: url("@/assets/images/icon/title-home-chosed-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon2 {
    background: url("@/assets/images/icon/title-comprehensiveReview-chosed-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon3 {
    background: url("@/assets/images/icon/title-powerAccess-chosed-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon4 {
    background: url("../../assets/images/icon/title-plan-chosed-icon.png")
      center no-repeat;
    background-size: 100%;
  }
  .titleIcon5 {
    background: url("@/assets/images/icon/title-investManege-chosed-icon.png") center
      no-repeat;
    background-size: 100%;
  }
  .titleIcon6 {
    background: url("@/assets/images/icon/title-allPlan-chosed-icon.png") center
      no-repeat;
    background-size: 100%;
  }
}
</style>

注意事项:

 使用的方法:

方式二:通过点击事件控制index,从而实现点击效果

代码如下:

<template>
  <div>
    <div>
      <div
        style="
          margin-left: 560px;
          display: flex;
          align-items: center;
          height: 87px;
          padding: 17px 0;
          box-sizing: border-box;
          cursor: pointer;
        "
      >
        <div
          @click="chosedTitle(item.num)"
          v-for="(item, index) in titleData"
          :key="index"
          style="
            display: flex;
            justify-content: center;
            align-items: center;
            margin-right: 76px;
          "
          :class="{
            twoWordChosedBackground:
              item.num == chosedIndex && item.title.length == 2,
            fourWordChosedBackground:
              item.num == chosedIndex && item.title.length == 4,
            fiveWordChosedBackground:
              item.num == chosedIndex && item.title.length == 5,
          }"
        >
          <img
            :src="item.num == chosedIndex ? item.chosedIcon : item.icon"
            style="width: 24px; height: 24px"
            alt=""
          />
          <span
            style="
              font-size: 18px;
              font-family: PingFang SC;
              font-weight: 500;
              line-height: 0px;
            "
            :style="{ color: item.num == chosedIndex ? '#FF942C' : '#00d8ff' }"
            >{{ item.title }}</span
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
    name:"title-index",
    data() {
        return {
            chosedIndex: 0,
            titleData: [
                {
                    title: "首页",
                    icon: require("@/assets/images/icon/title-home-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-home-chosed-icon.png"),
                    num: 0,
                },
                {
                    title: "综合评审",
                    icon: require("@/assets/images/icon/title-comprehensiveReview-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-comprehensiveReview-chosed-icon.png"),
                    num: 1,
                },
                {
                    title: "电源接入",
                    icon: require("@/assets/images/icon/title-powerAccess-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-powerAccess-chosed-icon.png"),
                    num: 2,
                },
                {
                    title: "月度计划",
                    icon: require("@/assets/images/icon/title-plan-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-plan-chosed-icon.png"),
                    num: 3,
                },
                {
                    title: "投资管理",
                    icon: require("@/assets/images/icon/title-investManege-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-investManege-chosed-icon.png"),
                    num: 4,
                },
                {
                    title: "项目全流程",
                    icon: require("@/assets/images/icon/title-allPlan-icon.png"),
                    chosedIcon: require("@/assets/images/icon/title-allPlan-chosed-icon.png"),
                    num: 5,
                },
            ],
        };
    },
    methods: {
        chosedTitle(val){ // 点击选中的值
            this.chosedIndex = val;
        },
    }
};
</script>

<style scoped>
.fiveWordChosedBackground{
     width: 146px;
    height: 32px;
    background: url('../../assets/images/icon/fiveWord-chosed-background.png') center no-repeat;
    background-size: 100%
}
.twoWordChosedBackground{
    width: 92px;
    height: 32px;
    background: url('../../assets/images/icon/twoWord-chosed-background.png') center center no-repeat;
    background-size: 100%
}
.fourWordChosedBackground{
    width: 128px;
    height: 32px;
    background: url('../../assets/images/icon/fourWord-chosed-background.png') center center no-repeat;
    background-size: 100%
}
</style>

此方法过于笨拙,不建议使用此方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值