实现效果:
方式一:利用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>
此方法过于笨拙,不建议使用此方法。