在我们做管理后台亦或是产品流程时,总是需要一个菜单栏或者导航栏来架起我们的产品架构,那么,如何用vue实现侧边折叠导航栏呢?接下来将一一介绍。
请先看效果演示图。
步骤1:先架构整个页面的布局,撰写html、css和js
HTML代码
数据中心后台管理
-
{{item.title}}
-
{{it.name}}
admin
JS代码
CSS代码
#main {
background: #F0F6FF;
font-size: 0.16rem;
}
#main .content {
display: flex;
flex-direction: row;
}
.content .admin_nav {
min-height: 9.37rem;
max-height: 1.3rem;
padding: 0 !important;
background: #364150;
position: relative;
}
.content .admin_nav .admin_nav_site{
height: 100vh;
}
.content .admin_nav .title {
height: 1.39rem;
line-height: 1.39rem;
background: rgba(63, 73, 86, 1);
color: #FFFFFF;
text-align: center;
font-size: 0.18rem;
display: flex;
justify-content: center;
}
.content .admin_nav .title div {
padding: 0 0.03rem;
}
.content .admin_nav .title div:first-child i {
font-size: 0.18rem;
}
.content .admin_nav ul .active {
background: rgba(68, 81, 99, 1);
}
.content .admin_nav ul .active1 a.router-link-active {
color: #fff;
}
.content .admin_nav ul li {
height: 0.6rem;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 0.18rem;
}
.content .admin_nav ul li a {
color: #cecdc5;
cursor: pointer;
}
.content .admin_nav ul li:first-child {
font-size: 0.2rem;
height: 0.64rem;
display: flex;
flex-direction: row;
align-items: center;
color: #BEC5C0;
cursor: pointer;
}
.content .admin_nav ul li:first-child div {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
color: #BEC5C0;
}
.content .admin_nav ul li:first-child .left_icon {
width: 22%;
}
.content .admin_nav ul li:first-child .left_icon i {
color: #1296DB;
}
.content .admin_nav ul li:first-child .middle {
width: 56%;
display: flex;
justify-content: flex-start;
}
.content .admin_nav ul li:first-child .right_icon {
width: 22%;
}
.admin_nav .admin .middle:hover {
color: #fff;
}
.admin_nav .admin .right_icon i:hover {
color: #fff;
}
.admin_nav .admin .middle.router-link-active {
color: #fff;
}
.content .admin_nav_position {
position: absolute;
z-index: 3000;
}
.content .admin_content {
padding: 0 !important;
}
.content .admin_content .admin_header {
width: 100%;
height: 0.6rem;
background: #364150;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.content .admin_content .admin_header .H5_item {
width: 0.4rem;
height: 0.4rem;
border-radius: 0.2rem;
display: flex;
justify-content: center;
align-items: center;
margin-left: 0.2rem;
}
.content .admin_content .admin_header .H5_item i {
font-size: 0.22rem;
color: #fff;
}
.content .admin_content .admin_header .user {
width: 100%;
display: flex;
align-items: center;
justify-content: flex-end;
}
.content .admin_content .admin_header .user .userHead {
height: 0.6rem;
display: flex;
flex-direction: row;
align-items: center;
color: #fff;
font-size: 0.18rem;
}
.content .admin_content .admin_header .user .userHead span {
padding: 0 0.05rem;
}
.content .admin_content .admin_header .user .userHead span:nth-child(1) {
position: relative;
}
.content .admin_content .admin_header .user .userHead span img {
width: 0.4rem;
height: 0.4rem;
border-radius: 50%;
}
.content .admin_content .admin_header .user .userHead span input {
width: 0.4rem;
height: 0.4rem;
position: absolute;
overflow: hidden;
opacity: 0;
cursor: pointer;
}
.content .admin_content .admin_header .user .logOut {
color: #fff;
margin-left: 0.12rem;
margin-right: 0.5rem;
}
.content .admin_content .admin_header .user .logOut i {
color: #1296DB;
font-size: 0.24rem;
cursor: pointer;
}
.toggle-cart-enter-active {
transition: all 0.4s linear;
}
.toggle-cart-leave-active {
transition: all 0.4s linear;
}
.toggle-cart-enter {
transform: translateX(-200%);
}
.toggle-cart-leave-active {
transform: translateX(-200%);
}
步骤2:撰写完基本网页布局和样式后,对部分细节进行剖析。
1、动态绑定路径
{{it.name}}
2、一级菜单,标题及折叠符号
①通过@mouseenter和@mouseleave事件,当鼠标移入和移出时,改变标题区域背景颜色
②通过@click事件,当鼠标点击时,显示二级菜单列表
③利用:class动态绑定类名,触发active方法,修改折叠符号状态;绑定图标类名,通过遍历,修改对应图标
-
{{item.title}}
-
{{it.name}}
active(index) {
this.currentSort = index;
},
changeTitleColor(index) {
this.currentSort = index;
},
removeTitleColor(index) {
this.currentSort = null;
},
handleToTitle(ind, item) {
this.navList[ind].isSubShow = !this.navList[ind].isSubShow;
if (this.navList[ind].isSubShow == true) {
this.navList[ind].close = 'icon-zhedie';
} else {
this.navList[ind].close = 'icon-close';
}
}
步骤3:菜单栏完成后,在网页端能进行查看,但是还需做一个手机端适配,这样就达到两端同步适应的效果。实现如下:
①对左边导航栏再撰写第二套代码,并绑定好对应的参数实现显示和隐藏
②运用vue已有的transition,绑定name值,实现动画效果绑定
HTML代码
数据中心后台管理
-
{{item.title}}
-
{{it.name}}
JS代码
mounted() {
window.onresize = () => { //监听屏幕变化
this.page_width();
};
this.page_width();
},
methods: {
make_menu() { //点击导航图标
this.dismenu = !this.dismenu;
},
page_width() { //获取屏幕宽度
var screenWidth = window.screen.width;
if (screenWidth <= 1024) {
this.fullWidth = false;
} else {
this.fullWidth = true;
}
}
}
最后来看下手机端效果:
附上全文代码:
数据中心后台管理
-
{{item.title}}
-
{{it.name}}
admin
数据中心后台管理
-
{{item.title}}
-
{{it.name}}
注:
1、css样式中的rem换算成px为对应的rem值*100,如:font-size:0.24rem;=> font-size:24px;
2、文章中涉及到的iconfont类名是因为引用了阿里图标,如遇到不知道如何引用的可以翻阅博文→ https://blog.csdn.net/weixin_44803753/article/details/113423633
3、关于router-link对应的跳转,如有小伙伴不太清楚如何使用vue-router配置路由的可以翻阅博文→https://blog.csdn.net/weixin_44803753/article/details/113436625
Ending(〃'▽'〃)!