1,效果
2,源码
链接:https://pan.baidu.com/s/1xPR8sp6UkZhSWM8tz7IrGw?pwd=nqwa
提取码:nqwa
–来自百度网盘超级会员V4的分享
3,视频演示
【仿导航栏(vue组件)】 https://www.bilibili.com/video/BV1ho4y1g7yH/?share_source=copy_web&vd_source=58607fc67d165b8caff1ed8bfcefd35b
4,代码
navBar.vue
<template>
<div class="wrapper" :style="{'--width':width,'--height':height,'--fontsize':fontsize,'--paddingleft':paddingleft,'--paddingtop':paddingtop,
'--backgroundcolor':backgroundcolor}">
<div class="one" :style="{'color':oneungetstatus,}" @click="onechangestatus">
<i class="el-icon-s-home" ></i>
{{name1}}
</div>
<div class="two" :style="{'color':twoungetstatus}" @click="twochangestatus">
<i class="el-icon-s-promotion"></i>
{{name2}}
</div>
<div class="three" :style="{'color':threeungetstatus}" @click="threechangestatus">
<i class="el-icon-user-solid"></i>
{{name3}}
</div>
<div class="three" :style="{'color':fourungetstatus}" @click="fourchangestatus">
<i class="el-icon-user"></i>
{{name4}}
</div>
<div class="three" :style="{'color':fiveungetstatus}" @click="fivechangestatus">
<i class="el-icon-user-solid"></i>
{{name5}}
</div>
<div class="three" :style="{'color':sixungetstatus}" @click="sixchangestatus">
<i class="el-icon-video-camera-solid"></i>
{{name6}}
</div>
<div class="three" :style="{'color':sevenungetstatus}" @click="sevenchangestatus">
<i class="el-icon-view"></i>
{{name7}}
</div>
<div class="three" :style="{'color':eightungetstatus}" @click="eightchangestatus">
<i class="el-icon-loading"></i>
{{name8}}
</div>
</div>
</template>
<!--可以更改参数有6个
//公用列的width(只有一列)
width:'',
//公用行的height(8行,每行的height都一样)
height:'',
//字体大小
fontsize:'',
//字体的paddingleft
paddingleft:'',
//字体的paddingtop
paddingtop:'',
//每行的background-color
backgroundcolor:'antiquewhite'
-->
<script>
export default {
name: "navBar",
data(){
return{
// //公用列的width (只有一列)
// width:'',
// //公用行的height 公用行的height(8行,每行的height都一样)
// height:'',
//第一行选中status的颜色
onegetstatus:'rgba(245,255,255,0.85)',
//第一行未选中状态的颜色
oneungetstatus:'grey',
//第二行选中status的颜色
twogetstatus:'rgb(245,255,255)',
//第二行未选中
twoungetstatus:'grey',
//第三行选中
threegetstatus:'rgb(255, 255, 255)',
//第三行未选中
threeungetstatus:'grey',
//第四行选中
fourgetstatus:'rgb(255, 255, 255)',
//第四行未选中
fourungetstatus:'grey',
//第五行选中
fivegetstatus:'rgb(255, 255, 255)',
//第五行未选中
fiveungetstatus:'grey',
//第六行选中
sixgetstatus:'rgb(255, 255, 255)',
//第六行未选中
sixungetstatus:'grey',
//第七行选中
sevengetstatus:'rgb(255, 255, 255)',
//第七行未选中
sevenungetstatus:'grey',
//第八行选中
eightgetstatus:'rgb(255, 255, 255)',
//第八行未选中
eightungetstatus:'grey',
// // //字体大小
// fontsize:'',
// //字体的paddingleft
// paddingleft:'',
// //字体的paddingtop
// paddingtop:'',
// //每行的background-color
// backgroundcolor:'antiquewhite'
}
},
props:['width','height','fontsize','paddingleft','paddingtop','backgroundcolor','name1','name2','name3',
'name4','name5','name6','name7','name8'],
methods:{
//第一行选中和未选中状态之间切换
onechangestatus(){
this.oneungetstatus = this.onegetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.twoungetstatus = unget
this.threeungetstatus = unget
this.fourungetstatus = unget
this.fiveungetstatus = unget
this.sixungetstatus = unget
this.sevenungetstatus = unget
this.eightungetstatus = unget
},
//第二行选中和未选中切换
twochangestatus(){
this.twoungetstatus = this.twogetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.oneungetstatus = unget
this.threeungetstatus = unget
this.fourungetstatus = unget
this.fiveungetstatus = unget
this.sixungetstatus = unget
this.sevenungetstatus = unget
this.eightungetstatus = unget
},
//第三行选中和未选中切换
threechangestatus(){
this.threeungetstatus = this.threegetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.twoungetstatus = unget
this.oneungetstatus = unget
this.fourungetstatus = unget
this.fiveungetstatus = unget
this.sixungetstatus = unget
this.sevenungetstatus = unget
this.eightungetstatus = unget
},
//第四行选中和未选中切换
fourchangestatus(){
this.fourungetstatus = this.fourgetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.twoungetstatus = unget
this.threeungetstatus = unget
this.oneungetstatus = unget
this.fiveungetstatus = unget
this.sixungetstatus = unget
this.sevenungetstatus = unget
this.eightungetstatus = unget
},
//第五行选中和未选中切换
fivechangestatus(){
this.fiveungetstatus = this.fivegetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.twoungetstatus = unget
this.threeungetstatus = unget
this.fourungetstatus = unget
this.oneungetstatus = unget
this.sixungetstatus = unget
this.sevenungetstatus = unget
this.eightungetstatus = unget
},
//第六行选中和未选中切换
sixchangestatus(){
this.sixungetstatus = this.sixgetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.twoungetstatus = unget
this.threeungetstatus = unget
this.fourungetstatus = unget
this.fiveungetstatus = unget
this.oneungetstatus = unget
this.sevenungetstatus = unget
this.eightungetstatus = unget
},
//第七行选中和未选中切换
sevenchangestatus(){
this.sevenungetstatus = this.sevengetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.twoungetstatus = unget
this.threeungetstatus = unget
this.fourungetstatus = unget
this.fiveungetstatus = unget
this.sixungetstatus = unget
this.oneungetstatus = unget
this.eightungetstatus = unget
},
//第八行选中和未选中切换
eightchangestatus(){
this.eightungetstatus = this.eightgetstatus
//未选中的颜色
var unget = 'grey'
// eslint-disable-next-line no-self-assign
this.twoungetstatus = unget
this.threeungetstatus = unget
this.fourungetstatus = unget
this.fiveungetstatus = unget
this.sixungetstatus = unget
this.sevenungetstatus = unget
this.oneungetstatus = unget
},
},
}
</script>
<style scoped>
:root{
/* 公用列的width*/
--width:'';
/* 公用行的height*/
--height:'';
/* 导航栏中的字体大小*/
--fontsize:'';
/* 字体的padding-top*/
--paddingtop:'';
/* 字体的paddingleft*/
--paddingleft:'';
/* 每行的background-color*/
--backgroundcolor:'';
}
.wrapper{
padding-top: 30px;
display: grid;
grid-template-columns: var(--width);
grid-gap: 10px;
grid-template-rows: var(--height) var(--height) var(--height) var(--height)
var(--height) var(--height) var(--height) var(--height);
}
.one{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
font-size: var(--fontsize);
transition: transform 0.3s ease;
background-color: var(--backgroundcolor);
}
.one:hover{
box-shadow: 0 0 10px gray;
transform: scale(0.98);
}
.two{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
font-size: var(--fontsize);
transition: transform 0.3s ease;
background-color: var(--backgroundcolor);
}
.two:hover{
box-shadow: 0 0 10px gray;
transform: scale(0.98);
}
.three{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
font-size: var(--fontsize);
transition: transform 0.3s ease;
background-color: var(--backgroundcolor);
}
.three:hover{
box-shadow: 0 0 10px gray;
transform: scale(0.98);
}
.four{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
color: gray;
}
.five{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
color: gray;
}
.six{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
color: gray;
}
.seven{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
color: gray;
}
.eight{
padding-left: var(--paddingleft);
padding-top: var(--paddingtop);
font-weight:bold;
color: gray;
}
/*!* 设置大屏幕的字体大小为20px *!*/
/*@media screen and (min-width: 1200px) {*/
/* .one {*/
/* font-size: 13px;*/
/* }*/
/* .two{*/
/* font-size: 13px;*/
/* }*/
/* .three{*/
/* font-size: 13px;*/
/* }*/
/*}*/
/*!* 设置中等屏幕的字体大小为18px *!*/
/*@media screen and (max-width: 1200px) and (min-width: 768px) {*/
/* .one {*/
/* font-size: 7px;*/
/* }*/
/* .two{*/
/* font-size: 7px;*/
/* }*/
/* .three{*/
/* font-size: 7px;*/
/* }*/
/*}*/
</style>
homeWl.vue
<template>
<div>
<nav-bar :width="width" :height="height" :fontsize="fontsize" :paddingleft="paddingleft"
:paddingtop="paddingtop" :backgroundcolor="backgroundcolor"
:name1="name1" :name2="name2" :name3="name3" :name4="name4"
:name5="name5" :name6="name6" :name7="name7" :name8="name8"></nav-bar>
</div>
</template>
<!--可以更改参数有6个 另外有8个name1,name2...等传入自己自定义的名字
//公用列的width(只有一列)
width:'',
//公用行的height(8行,每行的height都一样)
height:'',
//字体大小
fontsize:'',
//字体的paddingleft
paddingleft:'',
//字体的paddingtop
paddingtop:'',
//每行的background-color
backgroundcolor:'antiquewhite'
-->
<script>
import NavBar from "@/components/navBar/navBar";
export default {
name: "homeWl",
data(){
return{
//公用列的width (只有一列)
width:'',
//公用行的height 公用行的height(8行,每行的height都一样)
height:'',
//字体大小
fontsize:'',
//字体的paddingleft
paddingleft:'',
//字体的paddingtop
paddingtop:'',
//每行的background-color
backgroundcolor:'antiquewhite',
name1:'首页',
name2:'推荐',
name3:'关注',
name4:'朋友',
name5:'我的',
name6:'直播',
name7:'放映',
name8:'热点'
}
},
components: {NavBar},
mounted() {
this.gethomeheight()
this.gethomewidth()
window.addEventListener('resize', this.gethomewidth); // 在窗口大小改变时重新获取窗口宽度
window.addEventListener('resize', this.gethomeheight);
},
methods:{
gethomewidth(){
// 获取窗口宽度
var windowwidth = window.innerWidth
//公用列width(只有一列)
this.width = parseInt(windowwidth/5/4)+'px'
//字体大小
this.fontsize = parseInt(windowwidth/5/4/5)+'px'
//字体的paddingleft
this.paddingleft = parseInt(windowwidth/5/4/5)+'px'
//字体的paddingtop
this.paddingtop = parseInt(windowwidth/5/4/6)+'px'
},
// 获得窗口height度
gethomeheight(){
var windowheight = window.innerHeight
//公用行的height(公用8行,每行height都一样)
this.height = parseInt(windowheight/9/2)+'px'
}
},
}
</script>
<style scoped>
</style>
参数解释
可以更改参数有6个 另外有8个name1,name2...等传入自己自定义的名字
//公用列的width(只有一列)
width:'',
//公用行的height(8行,每行的height都一样)
height:'',
//字体大小
fontsize:'',
//字体的paddingleft
paddingleft:'',
//字体的paddingtop
paddingtop:'',
//每行的background-color
backgroundcolor:'antiquewhite'