element导航栏的样式修改、包括子菜单的样式、但是还未实现子菜单根据父级设置成不同宽度
效果图:
代码部分:
<div id="nav" :style="headStyle" v-cloak>
<div class="nav wrapper">
<div class="logo" :style="{backgroundImage:'url('+logoStyle+')'}"></div>
<div class="nav-content">
<el-menu background="transparent" :router="true" :default-active="activeIndex" class="el-menu-demo" mode="horizontal" :unique-opened="true"
@open="open">
<el-menu-item index="1"><a href="javascript:void(0);" class="font">首页</a></el-menu-item>
<el-submenu index="2" >
<template slot="title" ><a href="javascript:void(0);" class="font">产品&解决方案</a></template>
<el-menu-item index="2-1"><a class="item" href="javascript:void(0);">选项1</a></el-menu-item>
<el-menu-item index="2-2"><a class="item" href="javascript:void(0);">选项2</a></el-menu-item>
<el-menu-item index="2-3"><a class="item" href="javascript:void(0);">选项3</a></el-menu-item>
</el-submenu>
<el-submenu index="3">
<template slot="title"><a href="javascript:void(0);" class="font">关于</a></template>
<el-menu-item index="3-1"><a class="item" href="javascript:void(0);">公司新闻</a></el-menu-item>
<el-menu-item index="3-2"><a class="item" href="javascript:void(0);">客户动态</a></el-menu-item>
<el-menu-item index="3-3"><a class="item" href="javascript:void(0);">行业资讯</a></el-menu-item>
</el-submenu>
<el-submenu index="4" >
<template slot="title"><a href="javascript:void(0);" class="font">投资者关系</a></template>
<el-menu-item index="4-1"><a class="item" href="javascript:void(0);">选项1</a></el-menu-item>
<el-menu-item index="4-2"><a class="item" href="javascript:void(0);">选项2</a></el-menu-item>
<el-menu-item index="4-3"><a class="item" href="javascript:void(0);">选项3</a></el-menu-item>
</el-submenu>
<el-submenu index="5" >
<template slot="title"><a href="javascript:void(0);" class="font">联系我们</a></template>
<el-menu-item index="5-1"><a class="item" href="javascript:void(0);">选项1</a></el-menu-item>
<el-menu-item index="5-2"><a class="item" href="javascript:void(0);">选项2</a></el-menu-item>
<el-menu-item index="5-3"><a class="item" href="javascript:void(0);">选项3</a></el-menu-item>
</el-submenu>
<el-menu-item index="6"><a class="font" href="javascript:void(0);">生态合作</a></el-menu-item>
</el-menu>
</div>
</div>
</div>
css部分
``` #nav {
z-index: 999;
overflow: hidden;
width: 100%;
position: fixed;
top: 100px;
height: 100px;
background: white;
box-shadow: -10px 0px 30px 10px rgba(0,0,0,0.08);
}
.nav{
width: 1920px;
height: 100px;
overflow:hidden;
position: relative;
left: 50%;
transform: translateX(-50%);
background:transparent;
}
.nav .logo{
float: left;
width: 216px;
height: 53px;
margin:25px 40px 23px 360px;
background-size: cover;
}
.nav .nav-content{
float: left;
margin: 25px 0;
border:none
}
.nav a{
text-decoration: none;
}
.el-menu-demo{
background:transparent;
}
.el-menu.el-menu--horizontal{
border:none;
}
.el-menu.el-menu--horizontal .el-menu-item .is-active a{
font-size: 16px;
color:white;
}
/* 无下拉 */
::v-deep .el-menu--horizontal>.el-menu-item{
boder: none !important;
}
::v-deep .el-menu--horizontal>.el-menu-item a{
/* color: #333333; */
color: white;
font-size: 16px;
width: 100%;
height: 100%;
text-align: left;
}
/* 有下拉 */
::v-deep .nav .nav-content .el-menu--horizontal>.el-submenu .el-submenu__title {
/* color:#333; */
color: white;
font-size: 16px;
text-align: center;
border: none !important;
padding: 0 30px;
}
::v-deep .el-menu--horizontal>.el-submenu .el-submenu__title a{
/* color:#333; */
color: white;
/* width: 100%; */
height: 100%;
}
::v-deep .el-menu--horizontal>.el-submenu .el-submenu__title:hover {
background: #0078ff;
}
::v-deep .el-menu--horizontal>.el-submenu .el-submenu__title:hover a{
color:#ffffff !important
}
::v-deep .el-submenu.is-active .el-submenu__title{
border: none;
}
/* 生态 */
::v-deep .el-menu--horizontal > .el-menu-item a{
display: inline-block;
width: 100%;
height: 100%;
padding: 0 30px;
}
/*/首页 */
::v-deep .el-menu--horizontal>.el-menu-item.is-active{
border:none;
}
/* 二级菜单 */
::v-deep .el-submenu.is-opened .el-submenu__title {
background-color: #0078ff;
color: #ffffff;
}
::v-deep .el-submenu.is-opened .el-submenu__title i{
color: #ffffff;
}
::v-deep .el-submenu.is-opened .el-submenu__title a{
color:#ffffff !important
}
.el-menu--horizontal .el-menu-item:not(.is-disabled):hover{
background: #0078ff;
color: #ffffff;
}
.el-menu--horizontal .el-menu-item:not(.is-disabled):hover a{
color: #ffffff !important
}
.el-menu--horizontal .el-menu-item:not(.is-disabled) .item{
display: inline-block;
width: 100%;
height:100%;
text-decoration: none;
color: #333;
font-size: 16px;
font-family: Source Han Sans CN, Source Han Sans CN-Regular;
font-weight: 400;
text-align: center;
line-height: 36px;
text-shadow: -10px 0px 30px 10px rgba(0,0,0,0.08);
};
::v-deep .el-menu--horizontal .el-menu-item:hover{
background: #0078ff;
}
.el-menu--horizontal>.el-menu-item .is-active{
color:#333;
font-size: 16px;
padding: 0 30px;
}
::v-deep .el-menu--horizontal .el-menu--popup{
border-radius: 0px 0px 8px 8px !important;
}
::v-deep .el-menu--collapse .el-menu .el-submenu, .el-menu--popup{
min-width:0 !important;
}
.el-menu--horizontal>.el-submenu .el-submenu__title i{
color: #333;
}
/* icon */
::v-deep .el-icon-arrow-down:before {
content: "";
font-size: 20px;
}
::v-deep .el-menu--horizontal>.el-submenu .el-submenu__title:hover i{
color: #ffffff !important
}
::v-deep .el-submenu.is-active .el-submenu__title{
border: none;
}
::v-deep .el-menu--horizontal>.el-submenu .el-submenu__icon-arrow{
/* color: #333 */
color:rgb(255, 255, 255)
}
导航栏根据滚动变换字体以及左侧logo
``` name: 'one',
data () {
return {
opacity: 0,
activeIndex:'1',
top: 0,
headStyle: { background: 'transparent', color: 'rgba(0,0,0)' },
logoStyle: require('@/assets/logo@2x.png')
}
},
watch: {
'$router.path':
{handler(routerPath){
this.initMenuActive(routerPath)
},
immediate:true
}
,
top (val) {
const font = document.querySelectorAll('.font')
const icon = document.querySelectorAll('.el-submenu__icon-arrow')
if (val >= 20) {
this.headStyle.background = 'rgb(255,255,255)'
this.logoStyle = require('@/assets/logo.png')
for (let i = 0; i < font.length; i++) {
font[i].style.color = 'rgb(0,0,0)'
}
for (let i = 0; i < icon.length; i++) {
icon[i].style.color = 'rgb(0,0,0)'
}
}
if (val <= 20) {
this.headStyle.background = 'transparent'
this.logoStyle = require('@/assets/logo@2x.png')
for (let i = 0; i < font.length; i++) {
font[i].style.color = 'rgb(255,255,255)'
}
for (let i = 0; i < icon.length; i++) {
icon[i].style.color = 'rgb(255,255,255)'
}
}
}
},
mounted () {
window.addEventListener('scroll', this.scrolllHandle)
},
methods: {
initMenuActive(routerPath){
this.activeIndex=routerPath
},
scrolllHandle (e) {
this.top = e.srcElement.scrollingElement.scrollTop // 获取页面滚动高度
this.opacity = Math.abs(Math.round(this.top)) / 250
},
open (index) {
const element = document.querySelector('.el-menu--popup')
// const item = document.querySelector('.el-submenu__title')
// element.style.minWidth = item.style.width
// eslint-disable-next-line no-constant-condition
if (index === '3' || '5') {
element.style.minWidth = '152px'
} else if (index === '4') {
element.style.minWidth = '170px'
}
}
}